-
Notifications
You must be signed in to change notification settings - Fork 0
/
numtowords.cpp
62 lines (61 loc) · 1.29 KB
/
numtowords.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
using namespace std;
void expand(int);
int main()
{
int num;
cout<<"Enter a number : ";
cin>>num;
expand(num);
}
void expand(int value)
{
const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety"};
if(value<0)
{
cout<<"minus ";
expand(-value);
}
else if(value>=1000)
{
expand(value/1000);
cout<<" thousand";
if(value % 1000)
{
if(value % 1000 < 100)
{
cout << " and";
}
cout << " " ;
expand(value % 1000);
}
}
else if(value >= 100)
{
expand(value / 100);
cout<<" hundred";
if(value % 100)
{
cout << " and ";
expand (value % 100);
}
}
else if(value >= 20)
{
cout << tens[value / 10];
if(value % 10)
{
cout << " ";
expand(value % 10);
}
}
else
{
cout<<ones[value];
}
return;
}