/*Number To Words*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
char *numbers[30]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
char *weight[5]={"hundred","thousand","lakh","crore"};
void numberPrinter(int n)
{
int idx;
if(n>20)
{
idx=20+(n-20)/10;
printf("%s ",numbers[idx]);
}
else
printf("%s ",numbers[n]);
}
void numberMaker(char nos[3])
{
int l=strlen(nos);
int formnum;
if(l>1)
formnum=(nos[0]-48)*10+(nos[1]-48);
else
formnum=(nos[0]-48);
if(formnum>20 && formnum%10!=0)
{
numberPrinter((nos[0]-48)*10);
numberPrinter((nos[1]-48));
}
else
numberPrinter(formnum);
}
int sliceArray(char slc[3],char src[15],int index,int length)
{
int idx=0,flag=0;
while(length>0)
{
if(src[index]=='0')
flag++;
slc[idx]=src[index];
index++;idx++;
length--;
}
slc[idx]='\0';
return ((flag==2 && idx==2)?0:(flag==1 && idx==1)?0:1);
}
int main()
{
char num[15],slice[3];
int l,loop,idx_ct=0,ret_val;
printf("Enter a number to get the words reading\n");
scanf("%s",num);
l=strlen(num);
if(l>2)
{
loop=(int)ceil((l-3)/2.0);
while(loop>0)
{
if((l-3)%2==0)
{
ret_val=sliceArray(slice,num,idx_ct,2);
idx_ct=idx_ct+2;
l=l-2;
}
else
{
ret_val=sliceArray(slice,num,idx_ct,1);
idx_ct++;
l--;
}
if(ret_val==1)
{
numberMaker(slice);
printf("%s ",weight[loop]);
}
loop--;
}
if(sliceArray(slice,num,idx_ct,1))
{
numberMaker(slice);
printf("%s ",weight[loop]);
}
idx_ct++;
}
if(sliceArray(slice,num,idx_ct,2))
numberMaker(slice);
printf("\n");
system("pause");
return 1;
}
0 comments:
Post a Comment