-
Notifications
You must be signed in to change notification settings - Fork 96
/
spellTheNumber.c
115 lines (115 loc) · 2.91 KB
/
spellTheNumber.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
/*
* SPELL THE NUMBER
* 999999 = nine lakhs ninty nine thousand nine hundred ninty nine
*/
#include <stdio.h>
void intToWord(int num)
{
char a[20][50]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char b[8][50]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
char c[3][50]={"hundred","thousand","lakhs"};
if(num>=0 && num<20) //one digit
{
printf("%s",a[num]);
}
else if(num>19 && num<=99) //two digit
{
if(num%10==0) //20 30 40 50 60
{
num=num/10;
printf("%s",b[num-2]);
}
else //34 45 56 67
{
int d = num/10;
printf("%s",b[d-2]);
printf(" ");
intToWord(num%10);
}
}
else if(num>99 && num<=999) //three digit
{
if(num%100==0) //100 200 300 400
{
intToWord(num/100);
printf(" ");
printf("%s",c[0]);
}
else //111 222 333 444
{
intToWord(num/100);
printf(" ");
printf("%s",c[0]);
printf(" ");
intToWord(num%100);
}
}
else if(num>999 && num<=9999) //4 digit
{
if(num%1000==0) //1000 2000 3000 4000
{
intToWord(num/1000);
printf(" ");
printf("%s",c[1]);
}
else //1111 2222 3333 4444
{
intToWord(num/1000);
printf(" ");
printf("%s",c[1]);
printf(" ");
intToWord(num%1000);
}
}
else if(num>9999 && num<=99999) //5 digit
{
if(num%10000==0) // 10000, 20000, 30000, 40000
{
intToWord(num/1000);
printf(" ");
printf("%s",c[1]);
}
else if(num%1000==0) //32000 ,34000 ,45000
{
intToWord(num/1000);
printf(" ");
printf("%s",c[1]);
}
else //12300 ,12340, 12345,
{
intToWord(num/1000);
printf(" ");
printf("%s",c[1]);
printf(" ");
intToWord(num%1000);
}
}
else if(num>99999 && num<=999999)
{
if(num%100000==0) //100000 ,200000, 30000
{
intToWord(num/100000);
printf(" ");
printf("%s",c[2]);
}
else if(num%10000==0) //120000 ,230000, 450000
{
intToWord(num/10000);
printf(" ");
printf("%s",c[2]);
}
else //123000 ,123400 ,123450 ,123456
{
intToWord(num/100000);
printf(" ");
printf("%s",c[2]);
printf(" ");
intToWord(num%100000);
}
}
}
int main()
{
intToWord(999999);
printf("\n");
}