-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path9.c
96 lines (85 loc) · 2.52 KB
/
9.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
// Write program that reads the cost of an item in the form RRRR.PP (Where RRRR denotes Rupees and PP denotes Paise) and converts the value to a string of words that expresses the numeric value in words.
// For example, if we input 125.75, the output should be “ONE HUNDRED TWENTY FIVE AND PAISE SEVENTY FIVE”.
#include<stdio.h>
#include<string.h>
char* get_first_nine_number(int num)
{
switch(num)
{
case 0: return "ZERO";
case 1: return "ONE";
case 2: return "TWO";
case 3: return "THREE";
case 4: return "FOUR";
case 5: return "FIVE";
case 6: return "SIX";
case 7: return "SEVEN";
case 8: return "EIGHT";
case 9: return "NINE";
case 10: return "TEN";
case 11: return "ELEVEN";
case 12: return "TWELVE";
case 13: return "THIRTEEN";
case 14: return "FOURTEEN";
case 15: return "FIFTEEN";
case 16: return "SIXTEEN";
case 17: return "SEVENTEEN";
case 18: return "EIGHTEEN";
case 19: return "NINETEEN";
case 20: return "TWENTY";
case 30: return "THIRTY";
case 40: return "FOURTY";
case 50: return "FIFTY";
case 60: return "SIXTY";
case 70: return "SEVENTY";
case 80: return "EIGHTY";
case 90: return "NINETY";
}
}
char* get_number(int num)
{
char string[50];
if (num <= 20 )
return get_first_nine_number(num);
else
{
strcpy(string, "");
strcat(string, get_first_nine_number((num/10)*10));
strcat(string, " ");
strcat(string, get_first_nine_number(num%10));
char *string2 = string;
return(string2);
}
}
int main()
{
float cost;
printf("Enter the cost: ");
scanf("%f", &cost);
int rupees = (int)cost;
int paise = (int)((cost - rupees) * 100);
char rupees_string[100];
strcpy(rupees_string, "");
if ( rupees >= 1000 )
{
int thousands = rupees / 1000;
strcat(strcat(rupees_string, get_number(thousands)), " THOUSAND ");
rupees = rupees % 1000;
}
if ( rupees >= 100 )
{
int hundreds = rupees / 100;
strcat(strcat(rupees_string, get_number(hundreds)), " HUNDRED ");
rupees = rupees % 100;
}
if ( rupees >= 1 )
{
strcat(strcat(rupees_string, get_number(rupees)), " AND PAISE ");
}
if ( paise >= 1 )
{
strcat(rupees_string, get_number(paise));
}
printf("%s", rupees_string);
printf("\n%d", rupees);
}