# C++ program to convert number to word (up to four digits only)

Write a C++ program that will read an integer number (up to four digits) and convert it into words.
Submitted by Shubham Singh Rajawat, on June 12, 2017

Algorithm:

First of all we need to reverse the number using the method reverse() so that when we start converting we get the first digit first then next and so on.

There is another method print_c(), which has three arguments n which stores the current digit to be converted, l which stores the length which represent the ones tens thousand... place(length l is the main asset of this function for eg : if l =4 as soon as the current digit get processed l will get reduced by one so that the next digit get processed according to its place) , and result which stores the next digit to be processed.

So first the no is reversed using reverse() method then we process the digit one by one and print the word equivalent to it.

### Program to print number in words in C++

```#include<iostream>
#include<cmath>
using namespace std;

int reverse(int v,int lim)  /*Method to reverse the number*/
{
if(lim==1)
return v;
else
return (((v%10)*pow(10,lim-1))+reverse(v/10,lim-1));
}

void print_c(int digit,int l,int r=12)     /*Method to print word equivalent of a number*/
{
if(l!=2)                           /*l is the length of number */
{                                  /*digit is the digit being processed*/
switch(digit)                  /*TO print digit at ones place*/
{
case 1: cout<<"one ";
break;
case 2: cout<<"two ";
break;
case 3: cout<<"three ";
break;
case 4: cout<<"four ";
break;
case 5: cout<<"five ";
break;
case 6: cout<<"six ";
break;
case 7: cout<<"seven ";
break;
case 8: cout<<"eight ";
break;
case 9: cout<<"nine ";
break;
}
}
else if(l==2)                       /*to print digit at tens place*/
{
switch(digit)
{
case 1: switch(r)           /*TO print values such as ten,thirteen etc.*/
{
case 0: cout<<"ten";
break;
case 1: cout<<"eleven";
break;
case 2: cout<<"twelve";
break;
case 3: cout<<"thirteen";
break;
case 4: cout<<"fourteen";
break;
case 5: cout<<"fifteen";
break;
case 6: cout<<"sixteen";
break;
case 7: cout<<"seventeen";
break;
case 8: cout<<"eighteen";
break;
case 9: cout<<"nineteen";
break;
}
break;
case 2: cout<<"twenty ";
break;
case 3: cout<<"thirty ";
break;
case 4: cout<<"fourty ";
break;
case 5: cout<<"fifty ";
break;
case 6: cout<<"sixty ";
break;
case 7: cout<<"seventy ";
break;
case 8: cout<<"eighty ";
break;
case 9: cout<<"ninty ";
break;
case 0: cout<<"";
break;

}
}
}
int main()
{
int num,temp,length=0,result,n,m=0;
cout<<"Enter the number :";
cin>>num;
temp=num;

for(;num>0;num/=10)
{
length++;
}

result=reverse(temp,length);
while(result)
{
n=result%10;
m=m*10+n;
result/=10;
if(length==1)
{
print_c(n,length);       /*To print the digit at ones place*/
}
else if(length==2)
{
if(n==1)
{
print_c(n,length,result); /*To print the digit at tens place like ten,twelve etc.*/
break;
}
else
{
print_c(n,length);    /*To print the digit at tens place like twenty,thirty etc.*/
length--;
}
}
else if(length==3)
{
print_c(n,length);    /*To print the digit at hundred place*/
length--;
if(n!=0)
{
cout<<"hundred ";
}
}
else if(length==4)
{
print_c(n,length);       /*To print the digit at thousand place*/
length--;
cout<<"thousand ";
}
}

return 0;
}
```

Output

```First Run:
Enter the number :1005
One thousand five

Second Run:
Enter the number :01500
One thousand five hundred

Third Run:
Enter the number :1234
one thousand two hundred thirty four

```

What's New

Top Interview Coding Problems/Challenges!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates