Print integers in words using switch case..
#include<stdio.h>
char hash[10][10]={"","one","two","three","four","five","six","seven","eight","nine"};
char hash1[10][10]={"","ten","twenty","thrirty","forty","fifty","sixty","seventy","eighty","ninety"};
char hash11[10][10]={"ten","eleven","twelve","therteen","forteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
void convert(long long var)
{
long long pow=1;
while(var>=pow)
pow=pow*10;
pow=pow/10;
while(pow>0)
{
long long num=(var/pow)%10;
long long numhundred=((var*10)/pow)%100;
printf("%d",numhundred);
long long flag=0;
if(num!=0)
switch(pow)
{
case 1: printf("%s ",hash[num]);
break;
case 10: (num==1)? printf("%s ",hash11[numhundred-10]) , flag=1 :
printf("%s ",hash1[num]);
break;
case 100: printf("%s ",hash[num]) ; printf("hundred ");
if(var%100>0)printf("and ");
break;
case 1000:printf("%s ",hash[num]); printf("thousand ");
break;
case 10000: (num==1)? printf("%s ",hash11[numhundred-10]) , flag=1:
(numhundred%10>0) ? printf("%s ",hash1[num]),
printf("%s ",hash[numhundred%10]) , flag=1:
printf("%s ",hash1[num]); printf("thousand ");
break;
case 100000: printf("%s ",hash[num]); printf("lakhs ");
break;
case 1000000: printf("%s ",hash[num]); printf("million ");
break;
case 10000000: (num==1)? printf("%s ",hash11[numhundred-10]) , flag=1:
(numhundred%10>0) ? printf("%s ",hash1[num]),
printf("%s ",hash[numhundred%10]) , flag=1:
printf("%s ",hash1[num]); printf("million ");
break;
case 100000000: printf("%s ",hash[num]); printf("hundred ");
break;
default : printf("please add some more words in library");
return;
}
if(flag)
pow=pow/100;
else
pow=pow/10;
}
}
int main()
{
long long cases;
scanf("%lld",&cases);
while(cases--)
{
long long var;
scanf("%lld",&var);
convert(var);
puts("");
}
return 0;
}
Comments
Post a Comment