If you are looking for a C++ program to convert numbers to words, you can find a good C++ program here which can convert numbers to words up to 5 digits.
here is the program:
#include<iostream.h>
#include<conio.h>
void single(int);
void ten2ninety(int);
void elev2nine(int);
void dig2word(int);
void main()
{
clrscr();
int a;
cout<<"\n Enter a number\n";
cin>>a;
dig2word(a);
getch();
}
void dig2word(int y)
{
int s=y,ctr=0,ldig,sdig,l2dig,l3dig,l3rddig;
while(y>0)
{
y=y/10;
++ctr;
}
if(ctr==1)
{
single(s);
return;
}
else if(ctr==2)
{
ldig=s%10;
s=s/10;
sdig=s%10;
if(ldig==0)
{
ten2ninety(sdig);
return;
}
if(sdig==1)
{
elev2nine(ldig);
return;
}
ten2ninety(sdig);
single(ldig);
return;
}
else if(ctr==3)
{
l2dig=s%100;
s=s/100;
sdig=s%10;
if(l2dig==0)
{
single(sdig);
cout<<" Hundred";
return;
}
else
{
single(sdig);
cout<<" Hundred";
dig2word(l2dig);
return;
}
}
else if(ctr==4)
{
l3dig=s%1000;
s=s/1000;
sdig=s%10;
if(l3dig==0)
{
single(sdig);
cout<<" Thousand";
return;
}
else
{
single(sdig);
cout<<" Thousand";
dig2word(l3dig);
return;
}
}
else if(ctr==5)
{
l2dig=s%100;
l3dig=s%1000;
s=s/100;
l3rddig=s%10;
s=s/10;
if(l2dig==0&&l3rddig==0)
{
dig2word(s);
cout<<"Thousand";
return;
}
dig2word(s);
cout<<"Thousand";
dig2word(l3dig);
return;
}
}
void single(int x)
{
switch(x)
{
case 0:cout<<" Zero ";break;
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;
}
}
void elev2nine(int x)
{
switch(x)
{
case 1:cout<<" Eleven ";break;
case 2:cout<<" Tweleve ";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;
}
}
void ten2ninety(int x)
{
switch(x)
{
case 1:cout<<" Ten ";break;
case 2:cout<<" Twenty ";break;
case 3:cout<<" Thirty ";break;
case 4:cout<<" Forty ";break;
case 5:cout<<" Fifty ";break;
case 6:cout<<" Sixty ";break;
case 7:cout<<" Seventy ";break;
case 8:cout<<" Eighty ";break;
case 9:cout<<" Ninety ";break;
}
}
The above program works best for integer limit that means up to 32767 but if you want up to 99999. convert all "int" to "long int"
here is the program:
#include<iostream.h>
#include<conio.h>
void single(int);
void ten2ninety(int);
void elev2nine(int);
void dig2word(int);
void main()
{
clrscr();
int a;
cout<<"\n Enter a number\n";
cin>>a;
dig2word(a);
getch();
}
void dig2word(int y)
{
int s=y,ctr=0,ldig,sdig,l2dig,l3dig,l3rddig;
while(y>0)
{
y=y/10;
++ctr;
}
if(ctr==1)
{
single(s);
return;
}
else if(ctr==2)
{
ldig=s%10;
s=s/10;
sdig=s%10;
if(ldig==0)
{
ten2ninety(sdig);
return;
}
if(sdig==1)
{
elev2nine(ldig);
return;
}
ten2ninety(sdig);
single(ldig);
return;
}
else if(ctr==3)
{
l2dig=s%100;
s=s/100;
sdig=s%10;
if(l2dig==0)
{
single(sdig);
cout<<" Hundred";
return;
}
else
{
single(sdig);
cout<<" Hundred";
dig2word(l2dig);
return;
}
}
else if(ctr==4)
{
l3dig=s%1000;
s=s/1000;
sdig=s%10;
if(l3dig==0)
{
single(sdig);
cout<<" Thousand";
return;
}
else
{
single(sdig);
cout<<" Thousand";
dig2word(l3dig);
return;
}
}
else if(ctr==5)
{
l2dig=s%100;
l3dig=s%1000;
s=s/100;
l3rddig=s%10;
s=s/10;
if(l2dig==0&&l3rddig==0)
{
dig2word(s);
cout<<"Thousand";
return;
}
dig2word(s);
cout<<"Thousand";
dig2word(l3dig);
return;
}
}
void single(int x)
{
switch(x)
{
case 0:cout<<" Zero ";break;
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;
}
}
void elev2nine(int x)
{
switch(x)
{
case 1:cout<<" Eleven ";break;
case 2:cout<<" Tweleve ";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;
}
}
void ten2ninety(int x)
{
switch(x)
{
case 1:cout<<" Ten ";break;
case 2:cout<<" Twenty ";break;
case 3:cout<<" Thirty ";break;
case 4:cout<<" Forty ";break;
case 5:cout<<" Fifty ";break;
case 6:cout<<" Sixty ";break;
case 7:cout<<" Seventy ";break;
case 8:cout<<" Eighty ";break;
case 9:cout<<" Ninety ";break;
}
}
The above program works best for integer limit that means up to 32767 but if you want up to 99999. convert all "int" to "long int"
No comments:
Post a Comment