Print number in words
2 posters
Page 1 of 1
Print number in words
Write a program to print multi digit number in words.
Ex. I/P: 1583
O/P: one thousand five hundred eight tens three
I/P: 1043
O/P: one thousand four tens three
Ex. I/P: 1583
O/P: one thousand five hundred eight tens three
I/P: 1043
O/P: one thousand four tens three
Re: Print number in words
#include<stdio.h>
void main()
{
char a[10][10]={"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
char b[10][10]={"ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN",
"SEVENTEEN","EIGHTEEN","NINTEEN"};
char c[10][10]={"TEN","TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY",
"EIGHTY","NINTY"};
int no,t;
printf("ttEnter the number");
scanf("%d",&no);
printf("tt");
if(no<9999)
{
if(no>1000)
{
t=no/1000;
no=no%1000;
printf("%s THOUSAND",a[t-1]);
}
if(no>100)
{
t=no/100;
no=no%100;
printf(" %s HUNDRED AND ",a[t-1]);
}
if(no>=10 && no<20)
{
t=no/10;
printf(" %s",b[t-1]);
}
if(no>19 && no<=100)
{
t=no/10;
no=no%10;
printf("%s",c[t-1]);
}
if(no<10)
{
printf(" %s",a[no-1]);
}
}
else
printf("ttEnter the small number");
return (0);
}
void main()
{
char a[10][10]={"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
char b[10][10]={"ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN",
"SEVENTEEN","EIGHTEEN","NINTEEN"};
char c[10][10]={"TEN","TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY",
"EIGHTY","NINTY"};
int no,t;
printf("ttEnter the number");
scanf("%d",&no);
printf("tt");
if(no<9999)
{
if(no>1000)
{
t=no/1000;
no=no%1000;
printf("%s THOUSAND",a[t-1]);
}
if(no>100)
{
t=no/100;
no=no%100;
printf(" %s HUNDRED AND ",a[t-1]);
}
if(no>=10 && no<20)
{
t=no/10;
printf(" %s",b[t-1]);
}
if(no>19 && no<=100)
{
t=no/10;
no=no%10;
printf("%s",c[t-1]);
}
if(no<10)
{
printf(" %s",a[no-1]);
}
}
else
printf("ttEnter the small number");
return (0);
}
vinoveera- Posts : 7
Points : 22699
Join date : 2015-06-27
The answer to the program
#include<iostream.h>
void main()
{
int num,count=0,rem,tot=0;
cout<<"Enter the number to print in words: ";
cin>>num;
while(num>0)
{
rem=num%10;
tot=tot*10+rem;
num=num/10;
count++;
}
while(count>0)
{
rem=tot/10;
switch(rem)
{
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 ";
}
if(count==4&&rem!=0)
{
cout<<"Thousand ";
count--;
}
else if(count==3&&rem!=0)
{
cout<<"Hundred ";
count--;
}
else if(count==2&&rem!=0)
{
cout<<"Ten(s) ";
count--;
}
else if(count==1&&rem!=0)
{
cout<<"One(s) ";
count--;
}
tot=tot/10;
}
}
void main()
{
int num,count=0,rem,tot=0;
cout<<"Enter the number to print in words: ";
cin>>num;
while(num>0)
{
rem=num%10;
tot=tot*10+rem;
num=num/10;
count++;
}
while(count>0)
{
rem=tot/10;
switch(rem)
{
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 ";
}
if(count==4&&rem!=0)
{
cout<<"Thousand ";
count--;
}
else if(count==3&&rem!=0)
{
cout<<"Hundred ";
count--;
}
else if(count==2&&rem!=0)
{
cout<<"Ten(s) ";
count--;
}
else if(count==1&&rem!=0)
{
cout<<"One(s) ";
count--;
}
tot=tot/10;
}
}
Re: Print number in words
Hey My pgm also right la @admin
vinoveera- Posts : 7
Points : 22699
Join date : 2015-06-27
Re: Print number in words
vinoveera wrote:Hey My pgm also right la @admin
your program is absolutely perfect except only one error, you cannot return any value if its void. keep it up.. KUDOS!!!!
Similar topics
» Print second largest number
» Print palindrome number
» Print the sum of all digits
» Print fibonacci series
» Print palindrome number
» Print the sum of all digits
» Print fibonacci series
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|