-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberToWords.cpp
More file actions
84 lines (77 loc) · 1.8 KB
/
NumberToWords.cpp
File metadata and controls
84 lines (77 loc) · 1.8 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>
using namespace std;
string OneDigit(int n){
if(n==1) return "one";
if(n==2) return "two";
if(n==3) return "three";
if(n==4) return "four";
if(n==5) return "five";
if(n==6) return "six";
if(n==7) return "seven";
if(n==8) return "eight";
if(n==9) return "nine";
return "";
}
string Teens(int n){
if(n==10) return "ten";
if(n==11) return "eleven";
if(n==12) return "twelve";
if(n==13) return "thirteen";
if(n==14) return "fourteen";
if(n==15) return "fifteen";
if(n==16) return "sixteen";
if(n==17) return "seventeen";
if(n==18) return "eighteen";
if(n==19) return "nineteen";
return "";
}
string TwoDigits(int n){
if(n==2) return "twenty";
if(n==3) return "thirty";
if(n==4) return "forty";
if(n==5) return "fifty";
if(n==6) return "sixty";
if(n==7) return "seventy";
if(n==8) return "eighty";
if(n==9) return "ninety";
return "";
}
string NumbertoWords(int n){
if (n==0){
return "zero";
}
if(n<10){
return OneDigit(n);
}
if(n<20){
return Teens(n);
}
if(n<100){
int ones= n%10;
int tens= n/10;
if(ones==0){
return TwoDigits(tens);
}
else{
return TwoDigits(tens) +" "+ OneDigit(ones);
}
}
if(n<1000){
int hundreds= n/100;
int rest= n%100;
if(rest == 0){
return OneDigit(hundreds)+" hundred";
}
else{
return OneDigit(hundreds)+" hundred " + NumbertoWords(rest);
}
}
return "";
}
int main(){
int n;
cin>>n;
cout<<NumbertoWords(n)<<endl;
return 0 ;
}