How to write a C Program 0-1000 to Ordinary in C Programming Language ?
Solution For C Program :
/*C Program 0-1000 to Ordinary.*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
int main(void)
{
char buffer[5];
scanf("%4s", buffer);
char c;
if(fgetc(stdin) != '\n')
{
while((c=fgetc(stdin)) != '\n' && c!=EOF); // read rest of input
printf("input has been truncated to: %s\n", buffer);
}
if(strcmp(buffer, "0") == 0 || strcmp(buffer, "000") == 0 || strcmp(buffer, "0000") == 0 || strcmp(buffer, "0000") == 0)
{
printf("zero\n");
return 0;
}
int i = 0, n = strlen(buffer);
while(i<n)
{
char* res;
switch(n-i)
{
case 2:
switch(buffer[i] - '0')
{
case 0: res = ""; break;
case 1:
{
switch(buffer[i+1] - '0')
{
case 0: res = "ten"; break;
case 1: res = "eleven"; break;
case 2: res = "twelve"; break;
case 3: res = "thirteen"; break;
case 4: res = "fourteen"; break;
case 5: res = "fifteen"; break;
case 6: res = "sixteen"; break;
case 7: res = "seventeen"; break;
case 8: res = "eighteen"; break;
case 9: res = "nineteen"; break;
default: res = "error"; break;
}
i+=2;
continue;
}
break;
case 2: res = "twenty"; break;
case 3: res = "thirty"; break;
case 4: res = "forty"; break;
case 5: res = "fifty"; break;
case 6: res = "sixty"; break;
case 7: res = "seventy"; break;
case 8: res = "eighty"; break;
case 9: res = "ninety"; break;
default: res = "error"; break;
}
break;
default:
switch(buffer[i] - '0')
{
case 0: res = ""; break;
case 1: res = "one"; break;
case 2: res = "two"; break;
case 3: res = "three"; break;
case 4: res = "four"; break;
case 5: res = "five"; break;
case 6: res = "six"; break;
case 7: res = "seven"; break;
case 8: res = "eight"; break;
case 9: res = "nine"; break;
default: res = "error"; break;
}
break;
}
if(res)
{
printf("%s ", res);
res = "";
if(buffer[i] != '0')
{
switch(n-i)
{
case 4: res = "thousand "; break;
case 3: res = "hundred "; break;
case 2: break;
case 1: break;
default: break;
}
printf("%s", res);
}
}
++i;
}
printf("\n");
}