Chankey Pathak - 3 months ago 10
C Question

# Algorithm to take a number and output its English word

I want to make a program in C which will ask the user to input a number and then it will print that number in English.

For example:

``````if(INPUT == 1) then print ONE
if(INPUT == 2) then print TWO
``````

and so on. It can be made using switch-case and if else but it makes the code lengthy. For few numbers it's fine but if we have to write up to 100 then it will be lengthy.

Is there a short algorithm or idea for this?

You can use the below, but this prints only upto thousands. I did this to solve some particular programming problem. Thats why i did not extend beyond thousands. But its not hard to extend for bigger number. Also, this program can be still optimized or made more clearer.

``````#include <stdio.h>
#include <string.h>

void print(int num) {
char digit [21][10] = { "", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen"};
char tens [11][10] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};
char str[1000] = {0};
int prev=0, div=1000;
strcpy(str, "");

while(div) {

if ((num / div) % 10 > 0 || (div == 10 && (num%100) > 0)) {

if (prev) {
strcat(str, "and");
prev = 0;
}

switch(div) {
case 1000:
strcat(str, digit[(num / div) % 10]);
strcat(str, "thousand");
prev = 1;
break;
case 100:
strcat(str, digit[(num / div) % 10]);
strcat(str, "hundred");
prev = 1;
break;
case 10:
if ( (num%100) >= 10 && (num%100) <= 19)
strcat(str, digit[num%100]);
else {
strcat(str, tens[(num%100)/10]);
strcat(str, digit[num%10]);
}
break;
}
}

div /= 10;
}
printf("%d %s\n", num, str);

}
int main(int argc, char **argv) {

long sum = 0;
int count = 0;

if (argc <= 1) {
fprintf(stderr, "wrong number of arguments\n");
return -1;
}

print(atoi(argv[1]));

return 0;
}
``````