#include<stdio.h>
#include<conio.h>
#include<string.h>
char *single_digit[]={"","one","two","three","four","five","six","seven","eight","nine"};
char *two_digit[]={"ten","eleven","twelve","thirteen","fourteen","fiveteen","sixteen","seventeen","eighteen","nineteen"};
char *tens_multiple[]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
char *tens_power[]={"hundred","thousand"};
void one(char *num1)
{
printf("%s",single_digit[*num1-'0']);
}
void two(char *num2)
{
if(*num2=='0')
{
num2=num2+1;
one(num2);
}
else if(*(num2)=='1')
{
printf("%s",two_digit[*(num2+1)-'0']);
}
else
{
printf("%s ",tens_multiple[*num2-'0']);
num2=num2+1;
one(num2);
}
}
void three(char *num3)
{
if(*num3=='0')
{
num3=num3+1;
two(num3);
}
else
{
one(num3);
printf("%s "," hundred");
num3=num3+1;
two(num3);
}
}
void four(char *num4)
{
if(*num4=='0')
{
num4=num4+1;
three(num4);
}
else
{
one(num4);
printf("%s "," thousand");
num4=num4+1;
three(num4);
}
}
void convert(char *number)
{
//printf("\nNumber is %s length is %d",number,strlen(number));
while(*number=='0')
{
number=number+1;
}
//printf("\nnumber is %s length is %d",number,strlen(number));
int l=strlen(number);
//printf("\n first number is %s ",single_digit[0]);
switch(l)
{
case 1:
one(number);
break;
case 2:
two(number);
break;
case 3:
three(number);
break;
case 4:
four(number);
break;
default:
printf("\nOnly four digit numbers are allowed");
}
}
void main()
{
char *num;
printf("\n Enter a number ");
scanf("%s",num);
//printf("String is %s",num);
convert(num);
getch();
}
output
Enter a number 1230
one thousand two hundred thirty