-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer_to_words.c
79 lines (66 loc) · 2.37 KB
/
integer_to_words.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
#include <stdio.h>
#include <string.h>
int main() {
char number[10];
printf("Enter a number: ");
scanf("%s", number);
// print the number in words
printf("The number in words is: ");
char *ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char *tens[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
char *tens_power[] = {"hundred", "thousand"};
int len = strlen(number);
// if the number is 0
if (len == 1 && number[0] == '0') {
printf("%s", ones[0]);
}
// if the number is greater than 0 and less than 10
if (len == 1 && number[0] != '0') {
printf("%s", ones[number[0] - '0']);
}
// if the number is 20
if (len == 2 && number[0] == '2' && number[1] == '0') {
printf("%s", "twenty");
}
// if the number is greater than 10 and less than 20
if (len == 2) {
if (number[0] == '1') {
int sum = number[0] - '0' + number[1] - '0';
printf("%s", tens[sum]);
}
else if (number[0] == '2' && number[1] == '0') {
printf("%s", "twenty");
}
else {
printf("%s ", tens_multiple[number[0] - '0']);
printf("%s", ones[number[1] - '0']);
}
}
// if the number is greater than 20 and less than 100
if (len == 3) {
printf("%s ", ones[number[0] - '0']);
printf("%s ", tens_power[0]);
if (number[1] == '1') {
int sum = number[1] - '0' + number[2] - '0';
printf("%s", tens[sum]);
} else {
printf("%s ", tens_multiple[number[1] - '0']);
printf("%s", ones[number[2] - '0']);
}
}
// if the number is greater than 100 and less than 1000
if (len == 4) {
printf("%s ", ones[number[0] - '0']);
printf("%s ", tens_power[1]);
printf("%s ", ones[number[1] - '0']);
printf("%s ", tens_power[0]);
if (number[2] == '1') {
int sum = number[2] - '0' + number[3] - '0';
printf("%s", tens[sum]);
} else {
printf("%s ", tens_multiple[number[2] - '0']);
printf("%s", ones[number[3] - '0']);
}
}
}