# Numbers In Words

This is a discussion on Numbers In Words within the C Programming forums, part of the General Programming Boards category; Hi I have coded this code to print numbers up to 32767 in words. Well as a noob the job ...

1. ## Numbers In Words

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.

2. Not a bad solution. Kudos. Here's another way to go about it that you can generalize for huge numbers more easily. Break the number down into groups of 3 digits. Handle printing up to 999 for those three digits, then walk from the most significant group to the least, printing the appropriate qualifier after each group--like billion, million, and thousand--until you don't have any groups left. That way you break the problem down into the simpler problem of printing up to 999. Handling the grouping is cake after that.

When you see how easy it is, you'll wonder why you were restricted to 32767 in your exercise.

3. Actually i dont know the format string for double or float whatever is for integer value (as i have said i am noob) thats why this is restricted for max value a int can handle. Or else the code or the logic can support up to printing numbers upto 99999.
by the way thanks for ur idea I will work on it.