Hi
I have coded this code to print numbers up to 32767 in words.
Well as a noob the job was tough for me.I want you all to read my code and tell me any flaw of it or a better logic to do it.
Code:
// print numbers in word by 2d array
#include <stdio.h>
#include <conio.h>
int main(void)
{
char arr[10][6]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
char arr1[10][8]={"","Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
char arr2[10][10]={"","Eleven","Tewelv","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
int num; //input by user
int k[5]={0,0,0,0,0}; //each digit is stored in this to make there order correct.Max 5 digit 32767.Intialized to zero because no garbage is shown.
int i; //as usual for for loop
int flag=0; //for not print zero b4 any digit
clrscr();
scanf("%d",&num);
printf("The number is %d\n",num);
if (num==0)
printf("Zero");
else
{
for(i=4;num>0;i--)
{
k[i]=num%10;
num=num/10;
}
for(i=0;i<=4;i++)
{
if (k[i]>0 || flag==1)
{
if (i==4)
if(k[3]==1)
printf("%s ",arr2[k[i]]);
else
printf("%s ",arr[k[i]]); //k[i] will give digit from which arr[] will print its equ. word
else if (i==3)
if(k[3]==1 && k[4]>0)
{}
else
printf("%s ",arr1[k[i]]);
else if (i==2 && k[i]>0)
printf("%s Hundred ",arr[k[i]]);
else if (i==1)
{
if (k[0]==1)
{}
else
printf("%s ",arr[k[i]]);
printf("Thousand ");
}
else if (i==0)
{
if (k[0]==1 && k[1]>0)
printf("%s ",arr2[k[1]]);
else
printf("%s ",arr1[k[i]]);
flag=1;
}
}
}
}
getch();
return 0;
}
If some one feel uncomfortable for understanding it feel free to ask.