Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

To convert the given integer into a string

Aim:
To write a c++ program to convert the integer into a string
Algorithm:
Step1: Start the program
Step2: Initialize ones, seconds and tens
Step3: Get the number which has to be converted into string
Step4: Display the respective position of numbers and the corresponding strings to the
value entered
Step5: End the program
Program:
#include<iostream.h>
char *ones[10]={" One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine"};
char *seconds[10]={" Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen","
Seventeen"," Eighteen"," Nineteen"};
char *tens[10]={" Ten"," Twenty"," Thirty"," Forty"," Fifty"," Sixty"," Seventy"," Eighty","
Ninety"};
void main()
{
int number,res,terminate=1;
long int a,b,c,d,e;
while(terminate!=0)
{
cout << "Enter the number between 0-9999 : ";
cin >> number;
while(number<0 || number >10000)
{
cout << endl << "Error please enter the numbers between 0-9999 : ";

cin >> number;


}
a = number - (number%10000);
b = (number%10000) - (number%1000);
c = (number%1000) - (number%100);
d = (number%100) - (number%10);
e = number%10;
cout << "Numbers : "<< " " << b << " " << c << " " << d << " " << e << endl;
if(number==0)
{
cout << endl << " Zero";
}
if((a+b)==100000)
{
cout << tens[0];
}
if((a+b) > 10000 && (a+b) < 20000)
{
res = ((a+b)/1000)%10 - 1;
cout << seconds[res] << " " << " Thousand";
}
if(a>10000)
{
res = (a/10000)-1;
cout << tens[res];
}
if((a+b)%10000==0 && (a+b) > 99)

{
cout << " Thousand";
}
if((a+b)%10000!=0 && ((a+b)<10000 || (a+b)>20000))
{
res = b/1000 -1;
cout << ones[res] << " " << " Thousand";
}
if((a+b+c)%1000 != 0)
{
res = c/100 -1;
cout << ones[res] << " " << " Hundred";
}
if(d == 0 && e != 0)
{
res = e-1;
cout << ones[res];
}
if(d == 10 && e != 0)
{
res = e-1;
cout << seconds[res];
}
if((d+e)%10 == 0 && (d+e) != 0)
{
res = d/10 - 1;
cout << tens[res];

}
if((d+e) > 20 && (d+e)%10 != 0)
{
res = d/10 - 1;
cout << tens[res] << " " << ones[e-1];
}
if(number==10000)
{
cout << " (Above the range!!!) ";
}
cout << endl << "For running again press 1 or 0 for Exit : ";
cin >> terminate;
cout << endl;
}
}
Output:
Enter the number between 0-9999: 2345
Numbers: 2000 300 40 5
Two Thousand Three Hundrend and forty five
Result:
Thus the c++ program for to convert from integer to string was verified and executed
successfully.

You might also like