-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberInCharacters.c
247 lines (233 loc) · 5.86 KB
/
NumberInCharacters.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// For some reason the correct answer is only upto lacks.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int countDigits(long num)
{
int i=0;
while(num != 0)
{
i++;
num /= 10;
} return i;
}
int* numToArr(long num)
{
int digits = countDigits(num);
int *arr = (int*) malloc(digits*sizeof(int));
for(int i=0; i<digits; i++)
{
arr[(digits-i)-1] = num%10;
num /= 10;
}
return arr;
}
// function declairation
char* iToWord(unsigned long num);
char* numToWords(long num)
{
char* str = (char*) malloc(1024);
char* word;
int* temp = numToArr(num);
int digits = countDigits(num);
for(int i=0; i<digits; i++)
{
if(digits-i >= 13 && temp[i] != 0)
{
if(digits-i == 14)
if(temp[i] == 1) {
word = iToWord(10+temp[++i]);
strcat(word, "kharab ");
} else
word = iToWord(10*temp[i]);
else {
word = iToWord(temp[i]);
strcat(word, "kharab ");
}
} else if(digits-i >= 10 && temp[i] != 0)
{
if(digits-i == 11)
if(temp[i] == 1) {
word = iToWord(10+temp[++i]);
strcat(word, "arab ");
} else
word = iToWord(10*temp[i]);
else {
word = iToWord(temp[i]);
strcat(word, "arab ");
}
} else if(digits-i >= 8 && temp[i] != 0)
{
if(digits-i == 9)
if(temp[i] == 1) {
word = iToWord(10+temp[++i]);
strcat(word, "crore ");
} else
word = iToWord(10*temp[i]);
else {
word = iToWord(temp[i]);
strcat(word, "crore ");
}
} else if(digits-i >= 6 && temp[i] != 0)
{
if(digits-i == 7)
if(temp[i] == 1) {
word = iToWord(10+temp[++i]);
strcat(word, "lack ");
} else
word = iToWord(10*temp[i]);
else {
word = iToWord(temp[i]);
strcat(word, "lack ");
}
} else if(digits-i >= 4 && temp[i] != 0)
{
if(digits-i == 5)
if(temp[i] == 1) {
word = iToWord(10+temp[++i]);
strcat(word, "thousand ");
} else
word = iToWord(10*temp[i]);
else {
word = iToWord(temp[i]);
strcat(word, "thousand ");
}
} else if(digits-i == 3 && temp[i] != 0)
{
word = iToWord(temp[i]);
strcat(word, "hundred ");
} else if(temp[i] == 1 && digits-i > 1)
{
word = iToWord(10+temp[++i]);
} else if(digits-i > 1) {
word = iToWord(10*temp[i]);
} else
word = iToWord(temp[i]);
strcat(str, word);
free(word);
}
free(temp);
return str;
}
int main(){
int n;
printf("Enter the number: \n");
scanf("%d", &n);
char* a = numToWords(n);
printf("%s", a);
free(a);
return 0;
}
// function definition
char* iToWord(unsigned long num)
{
char* str = malloc(20);
strcpy(str, "");
switch(num)
{
// once
case 1:
strcat(str, "one ");
break;
case 2:
strcat(str, "two ");
break;
case 3:
strcat(str, "three ");
break;
case 4:
strcat(str, "four ");
break;
case 5:
strcat(str, "five ");
break;
case 6:
strcat(str, "six ");
break;
case 7:
strcat(str, "seven ");
break;
case 8:
strcat(str, "eight ");
break;
case 9:
strcat(str, "nine ");
break;
case 10:
strcat(str, "ten ");
break;
case 11:
strcat(str, "eleven ");
break;
case 12:
strcat(str, "twelve ");
break;
case 13:
strcat(str, "thirteen ");
break;
case 14:
strcat(str, "fourteen ");
break;
case 15:
strcat(str, "fifteen ");
break;
case 16:
strcat(str, "sixteen ");
break;
case 17:
strcat(str, "seventeen ");
break;
case 18:
strcat(str, "eighteen ");
break;
case 19:
strcat(str, "ninteen ");
break;
// tens
case 20:
strcat(str, "twenty ");
break;
case 30:
strcat(str, "thirty ");
break;
case 40:
strcat(str, "fourty ");
break;
case 50:
strcat(str, "fifty ");
break;
case 60:
strcat(str, "sixty ");
break;
case 70:
strcat(str, "seventy ");
break;
case 80:
strcat(str, "eighty ");
break;
case 90:
strcat(str, "ninety ");
break;
// place
case 100:
strcat(str, "hundred ");
// break;
// case 1000:
// strcat(str, "thousand ");
// break;
// case 100000:
// strcat(str, "lack ");
// break;
// case 10000000:
// strcat(str, "crore ");
// break;
// case 1000000000:
// strcat(str, "arab ");
// break;
// case 1000000000000:
// strcat(str, "kharab ");
// break;
}
return str;
}