|
1 | 1 | /* C program to print a given number in words. The program
|
2 | 2 | handles numbers from 0 to 9999
|
3 | 3 | Just take an example if the user privide the number
|
4 |
| -1234 -Input |
5 |
| -One thousand Two hundred and twenty four -Output |
6 |
| -
|
| 4 | +INPUT- |
| 5 | +1-No of test cases |
| 6 | +1234-the given number |
| 7 | +OUTPUT- |
| 8 | +One thousand Two hundred and twenty four */ |
| 9 | + |
| 10 | +#include <bits/stdc++.h> |
| 11 | +#define ff first |
| 12 | +#define ss second |
| 13 | +#define int long long |
| 14 | +#define pb push_back |
| 15 | +#define mp make_pair |
| 16 | +#define pii pair<int,int> |
| 17 | +#define vi vector<int> |
| 18 | +#define mii map<int,int> |
| 19 | +#define pqb priority_queue<int> |
| 20 | +#define pqs priority_queue<int,vi,greater<int> > |
| 21 | +#define setbits(x) __builtin_popcountll(x) |
| 22 | +#define zrobits(x) __builtin_ctzll(x) |
| 23 | +#define mod 1000000007 |
| 24 | +#define inf LONG_LONG_MAX |
| 25 | +#define ps(x,y) fixed<<setprecision(y)<<x |
| 26 | +#define endl '\n' |
| 27 | +#define mk(arr,n,type) type *arr=new type[n] |
| 28 | +#define w(x) int x; cin >> x; while(x--) |
| 29 | +#define f(i,x,y) for(int i = x; i < y; i++) |
| 30 | +#define g(i,x,y) for(int i = x;i<=y;i++) |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +using namespace std; |
| 35 | +string one[] = { "", "one ", "two ", "three ", "four ", |
| 36 | + "five ", "six ", "seven ", "eight ", |
| 37 | + "nine ", "ten ", "eleven ", "twelve ", |
| 38 | + "thirteen ", "fourteen ", "fifteen ", |
| 39 | + "sixteen ", "seventeen ", "eighteen ", |
| 40 | + "nineteen " }; |
| 41 | + |
| 42 | +// strings at index 0 and 1 are not used, they is to |
| 43 | +// make array indexing simple |
| 44 | +string ten[] = { "", "", "twenty ", "thirty ", "forty ", |
| 45 | + "fifty ", "sixty ", "seventy ", "eighty ", |
| 46 | + "ninety " }; |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +void c_p_p() |
| 51 | +{ |
| 52 | + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); |
7 | 53 |
|
| 54 | +} |
| 55 | +/* ******* Your Functions Below ******** */ |
8 | 56 |
|
9 | 57 |
|
10 |
| -*/ |
11 |
| -#include <stdio.h> |
12 |
| -#include <stdlib.h> |
13 |
| -#include <string.h> |
| 58 | +// n is 1- or 2-digit number |
| 59 | +string numToWords(int n, string s) |
| 60 | +{ |
| 61 | + string str = ""; |
| 62 | + // if n is more than 19, divide it |
| 63 | + if (n > 19) |
| 64 | + str += ten[n / 10] + one[n % 10]; |
| 65 | + else |
| 66 | + str += one[n]; |
| 67 | + |
| 68 | + // if n is non-zero |
| 69 | + if (n) |
| 70 | + str += s; |
| 71 | + |
| 72 | + return str; |
| 73 | +} |
14 | 74 |
|
15 |
| -/* A function that prints given number in words */ |
16 |
| -void convert_to_words(char* num) |
| 75 | +// Function to print a given number in words |
| 76 | +string convertToWords(long n) |
17 | 77 | {
|
18 |
| - int len = strlen( |
19 |
| - num); // Get number of digits in given number |
20 |
| - |
21 |
| - /* Base cases */ |
22 |
| - if (len == 0) { |
23 |
| - fprintf(stderr, "empty string\n"); |
24 |
| - return; |
25 |
| - } |
26 |
| - if (len > 4) { |
27 |
| - fprintf(stderr, |
28 |
| - "Length more than 4 is not supported\n"); |
29 |
| - return; |
30 |
| - } |
31 |
| - |
32 |
| - /* The first string is not used, it is to make |
33 |
| - array indexing simple */ |
34 |
| - char* single_digits[] |
35 |
| - = { "zero", "one", "two", "three", "four", |
36 |
| - "five", "six", "seven", "eight", "nine" }; |
37 |
| - |
38 |
| - /* The first string is not used, it is to make |
39 |
| - array indexing simple */ |
40 |
| - char* two_digits[] |
41 |
| - = { "", "ten", "eleven", "twelve", |
42 |
| - "thirteen", "fourteen", "fifteen", "sixteen", |
43 |
| - "seventeen", "eighteen", "nineteen" }; |
44 |
| - |
45 |
| - /* The first two string are not used, they are to make |
46 |
| - array indexing simple*/ |
47 |
| - char* tens_multiple[] = { "", "", "twenty", |
48 |
| - "thirty", "forty", "fifty", |
49 |
| - "sixty", "seventy", "eighty", |
50 |
| - "ninety" }; |
51 |
| - |
52 |
| - char* tens_power[] = { "hundred", "thousand" }; |
53 |
| - |
54 |
| - /* Used for debugging purpose only */ |
55 |
| - printf("\n%s: ", num); |
56 |
| - |
57 |
| - /* For single digit number */ |
58 |
| - if (len == 1) { |
59 |
| - printf("%s\n", single_digits[*num - '0']); |
60 |
| - return; |
61 |
| - } |
62 |
| - |
63 |
| - /* Iterate while num is not '\0' */ |
64 |
| - while (*num != '\0') { |
65 |
| - |
66 |
| - /* Code path for first 2 digits */ |
67 |
| - if (len >= 3) { |
68 |
| - if (*num - '0' != 0) { |
69 |
| - printf("%s ", single_digits[*num - '0']); |
70 |
| - printf("%s ", |
71 |
| - tens_power[len - 3]); // here len can |
72 |
| - // be 3 or 4 |
73 |
| - } |
74 |
| - --len; |
75 |
| - } |
76 |
| - |
77 |
| - /* Code path for last 2 digits */ |
78 |
| - else { |
79 |
| - /* Need to explicitly handle 10-19. Sum of the |
80 |
| - two digits is used as index of "two_digits" |
81 |
| - array of strings */ |
82 |
| - if (*num == '1') { |
83 |
| - int sum = *num - '0' + *(num + 1) - '0'; |
84 |
| - printf("%s\n", two_digits[sum]); |
85 |
| - return; |
86 |
| - } |
87 |
| - |
88 |
| - /* Need to explicitely handle 20 */ |
89 |
| - else if (*num == '2' && *(num + 1) == '0') { |
90 |
| - printf("twenty\n"); |
91 |
| - return; |
92 |
| - } |
93 |
| - |
94 |
| - /* Rest of the two digit numbers i.e., 21 to 99 |
95 |
| - */ |
96 |
| - else { |
97 |
| - int i = *num - '0'; |
98 |
| - printf("%s ", i ? tens_multiple[i] : ""); |
99 |
| - ++num; |
100 |
| - if (*num != '0') |
101 |
| - printf("%s ", |
102 |
| - single_digits[*num - '0']); |
103 |
| - } |
104 |
| - } |
105 |
| - ++num; |
106 |
| - } |
| 78 | + // stores word representation of given number n |
| 79 | + string out; |
| 80 | + |
| 81 | + // handles digits at ten millions and hundred |
| 82 | + // millions places (if any) |
| 83 | + out += numToWords((n / 10000000), "crore "); |
| 84 | + |
| 85 | + // handles digits at hundred thousands and one |
| 86 | + // millions places (if any) |
| 87 | + out += numToWords(((n / 100000) % 100), "lakh "); |
| 88 | + |
| 89 | + // handles digits at thousands and tens thousands |
| 90 | + // places (if any) |
| 91 | + out += numToWords(((n / 1000) % 100), "thousand "); |
| 92 | + |
| 93 | + // handles digit at hundreds places (if any) |
| 94 | + out += numToWords(((n / 100) % 10), "hundred "); |
| 95 | + |
| 96 | + if (n > 100 && n % 100) |
| 97 | + out += "and "; |
| 98 | + |
| 99 | + // handles digits at ones and tens places (if any) |
| 100 | + out += numToWords((n % 100), ""); |
| 101 | + |
| 102 | + return out; |
107 | 103 | }
|
108 | 104 |
|
109 |
| -/* Driver program to test above function */ |
110 |
| -int main(void) |
| 105 | +/* ******* Your Functions Above ******** */ |
| 106 | + |
| 107 | +int32_t main() |
| 108 | +{ |
| 109 | + c_p_p(); |
| 110 | + #ifndef ONLINE_JUDGE |
| 111 | + // For getting input from input.txt file |
| 112 | + // For getting input from input.txt file |
| 113 | + freopen("inpute.exe", "r", stdin); |
| 114 | + |
| 115 | + // Printing the Output to output.txt file |
| 116 | + freopen("output.exe", "w", stdout); |
| 117 | +#endif |
| 118 | + int t; |
| 119 | + cin>>t; |
| 120 | + |
| 121 | + while(t--) |
111 | 122 | {
|
112 |
| - convert_to_words("9923"); |
113 |
| - convert_to_words("523"); |
114 |
| - convert_to_words("89"); |
115 |
| - convert_to_words("8"); |
| 123 | + long n ; |
| 124 | + cin>>n; |
116 | 125 |
|
117 |
| - return 0; |
| 126 | + // convert given number in words |
| 127 | + cout << convertToWords(n) << endl; |
| 128 | + |
| 129 | + |
| 130 | +} |
| 131 | +return 0; |
118 | 132 | }
|
| 133 | +/*Time complexity: |
| 134 | +Time complexity: O(1). |
| 135 | +The loop runs for a constant amount of time. |
| 136 | +Auxiliary space: O(1). |
| 137 | +As no extra space is required. |
| 138 | +Time complexity: O(1). */ |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
0 commit comments