forked from ishita0302/Code-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Numbers to words.c
69 lines (61 loc) · 1.6 KB
/
Numbers 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
#include<string.h>
#include<stdio.h>
#define SIZE 20
char * words(long n, char *a);
char * numtowords(int n, char* b, char * c, int len);
//selecting strings
char* one[] = { "", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ",
"thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen "
};
char* ten[] = { "", "", "twenty ", "thirty ", "forty ",
"fifty ", "sixty ", "seventy ", "eighty ",
"ninety "
};
int main()
{
long num;
char str[100] = "\0";
printf("Enter any number: ");
scanf("%ld", &num);
//function calling
printf( "%s",words(num,str));
return 0;
}
// Function to print a given number in words
char * words(long n, char *a)
{
char str[SIZE] = "\0";
strcat(a, numtowords((n / 10000000), "crore ",str,SIZE));
strcat(a, numtowords(((n / 100000) % 100), "lakh ",str,SIZE));
strcat(a, numtowords(((n / 1000) % 100), "thousand ",str,SIZE));
strcat(a, numtowords(((n / 100) % 10), "hundred ",str,SIZE));
if (n > 100 && n % 100)
{
strcat(a, "and ");
}
strcat(a, numtowords((n % 100), "",str,SIZE));
return a;
}
//second function
char * numtowords(int n, char* b, char * c, int len)
{
memset(c,0,len);
if (n > 19)
{
strcat(c,ten[n / 10]);
strcat(c,one[n % 10]);
}
else
{
strcat(c,one[n]);
}
if (n)
{
strcat(c,b);
}
return c;
}