-
Notifications
You must be signed in to change notification settings - Fork 0
/
11thQuestion.cpp
74 lines (66 loc) · 2.01 KB
/
11thQuestion.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
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using namespace std;
class Numbers {
public:
Numbers(int n) {
number = n;
}
void print() {
if (number < 20) {
cout << lessthan20[number] << endl;
} else if (number < 100) {
cout << tens[number / 10 - 2];
if (number % 10 != 0) cout << " " << lessthan20[number % 10];
cout << endl;
} else if (number < 1000) {
cout << lessthan20[number / 100] << " hundred ";
if (number % 100 != 0) {
printLessThan100(number % 100);
} else {
cout << endl;
}
} else if (number < 10000) {
cout << lessthan20[number / 1000] << " thousand ";
if (number % 1000 != 0) {
printLessThan1000(number % 1000);
} else {
cout << endl;
}
}
}
private:
int number;
static string lessthan20[20];
static string tens[9];
void printLessThan100(int n) {
if (n < 20) {
cout << lessthan20[n] << endl;
} else {
cout << tens[n / 10 - 2];
if (n % 10 != 0) cout << " " << lessthan20[n % 10];
cout << endl;
}
}
void printLessThan1000(int n) {
if (n < 100) {
printLessThan100(n);
} else {
cout << lessthan20[n / 100] << " hundred ";
if (n % 100 != 0) {
printLessThan100(n % 100);
} else {
cout << endl;
}
}
}
};
string Numbers::lessthan20[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string Numbers::tens[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"};
int main() {
int n;
cout << "Enter a number between 0 and 9999: ";
cin >> n;
Numbers n1(n);
n1.print();
return 0;
}