Skip to content

Commit e418651

Browse files
authored
Merge pull request ashutosh97#246 from poojabasker/master
Added Numbers to Words in Basic Math
2 parents ddae6c1 + bdbcb0d commit e418651

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//Converts any number between 0-9999 (inclusive) to words
2+
//till 4 digits only
3+
4+
#include<conio.h>
5+
#include<iostream.h>
6+
7+
void words(int n, int c, int r)
8+
{
9+
if(n==0 && c==1)
10+
{
11+
cout<<"Zero"<<endl;
12+
return;
13+
}
14+
if(!r)
15+
return;
16+
17+
char singled[][10]={"one","two","three","four","five","six","seven","eight","nine"};
18+
char ones[][20]={"eleven","twelve","thirteen"};
19+
char doubled[][10]={"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
20+
// char threed[][10]={"hunderd","thousand"};
21+
22+
switch(c)
23+
{
24+
case 1: cout<<" "<<singled[r-1];
25+
break;
26+
case 2: if(n>=20 || n==10)
27+
{
28+
cout<<" "<<doubled[r%10-1];
29+
words(n/10,c-1,r/10);
30+
}
31+
else if(n>=14)
32+
cout<<" "<<singled[r%10-1]<<"teen";
33+
else
34+
cout<<" "<<ones[n%10-1];
35+
break;
36+
case 3: words(n,1,r%10);
37+
cout<<" "<<"hundred";
38+
words(n%100,c-1,r/10);
39+
break;
40+
case 4: words(n,1,r%10);
41+
cout<<" "<<"thousand";
42+
words(n%1000,c-1,r/10);
43+
break;
44+
}
45+
}
46+
void main()
47+
{
48+
clrscr();
49+
cout<<"Enter a number: ";
50+
int n;
51+
cin>>n;
52+
int n1=n;
53+
int count=0, rev=0;
54+
while(n1!=0)
55+
{
56+
rev=rev*10+(n1%10);
57+
count++;
58+
n1=n1/10;
59+
}
60+
words(n,count,rev);
61+
getch();
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Write code to convert a given number into words.
2+
For example,
3+
if “1234” is given as input
4+
output should be “one thousand two hundred thirty four”.

0 commit comments

Comments
 (0)