1
+ // Program to convert given number to words
2
+
3
+ #include < bits/stdc++.h>
4
+ using namespace std ;
5
+
6
+ string twoDigit (int num,map<int ,string> mp){
7
+ if (mp.find (num) != mp.end ())
8
+ return mp[num];
9
+ return mp[(num/10 )*10 ] + " " + mp[num%10 ];
10
+ }
11
+
12
+ string threeDigit (int num,map<int ,string> mp){
13
+ if (num / 100 && num % 100 )
14
+ return mp[num/100 ] + " Hundred " + twoDigit (num%100 ,mp);
15
+ else if (num/100 )
16
+ return mp[num/100 ] + " Hundred " ;
17
+ return twoDigit (num%100 ,mp);
18
+ }
19
+
20
+ string numberToWords (long int num,map<int ,string> mp){
21
+ if (!num)
22
+ return " Zero" ;
23
+
24
+ int part1 = num % 1000 ;
25
+ int part2 = (num / 1000 ) % 1000 ;
26
+ int part3 = (num / 1000000 ) % 1000 ;
27
+ int part4 = num / 1000000000 ;
28
+ string result = " " ;
29
+
30
+ if (part4)
31
+ result += twoDigit (part4,mp)+" Billion " ;
32
+ if (part3)
33
+ result += threeDigit (part3,mp)+" Million " ;
34
+ if (part2)
35
+ result += threeDigit (part2,mp)+" Thousand " ;
36
+ result += threeDigit (part1,mp);
37
+ return result;
38
+ }
39
+
40
+ int main (){
41
+ long int num;
42
+ map<int ,string> mp;
43
+ mp[1 ] = " One" ; mp[2 ] = " Two" ; mp[3 ] = " Three" ; mp[4 ] = " Four" ; mp[5 ] = " Five" ;
44
+ mp[6 ] = " Six" ; mp[7 ] = " Seven" ; mp[8 ] = " Eight" ; mp[9 ] = " Nine" ; mp[10 ] = " Ten" ;
45
+ mp[11 ] = " Eleven" ; mp[12 ] = " Twelve" ; mp[13 ] = " Thirteen" ; mp[14 ] = " Fourteen" ;
46
+ mp[15 ] = " Fifteen" ; mp[16 ] = " Sixteen" ; mp[17 ] = " Seventeen" ; mp[18 ] = " Eighteen" ;
47
+ mp[19 ] = " Nineteen" ; mp[20 ] = " Twenty" ; mp[30 ] = " Thirty" ; mp[40 ] = " Forty" ;
48
+ mp[50 ] = " Fifty" ; mp[60 ] = " Sixty" ; mp[70 ] = " Seventy" ; mp[80 ] = " Eighty" ;
49
+ mp[90 ] = " Ninety" ;
50
+
51
+ cin>>num;
52
+ string result = numberToWords (num,mp);
53
+ cout<<result;
54
+ }
0 commit comments