# Thread: converting integers to words

1. ## converting integers to words

this code is giving me stress....

Code:
```//FileName: int_converter.c
//Author: Nyah Check, Chairman && CEO at INK Corp.
//Usage: This program converts numbers between 0 and a billion to words.
//INK Corp. 2012.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 10

int analyse_print(int *, int);

int num[MAX];
int i;

//Main: This  function calls two main functions; read_int, analyse_int; respectively.
int main(void)
{
printf("\nEnter a number between 1 and a billion for it to be converted to words.");
printf("\nEnter 'Q' to QUIT.");

for( ; ; )
{
analyse_print(num, i);
}
return 0;
}

//read_int: This function reads the integers from input checks and stores them in an array.
{
int ch;

i = 0;
printf("\nEnter number Here: ");

while( (ch = getchar())!= '\n')
{

if ( ch == 'Q' || ch == 'q')
{
printf("\nExiting....");
printf("\nThanks for using our software; we would like to receive your comments and suggestions to better our services.");
printf("\nINK Corp. 2012.");
exit(EXIT_SUCCESS);
}

else if ( i < n)
{
num[i++] = ch;
}
}
printf("\n%d\n", i);

return i;
}

//analyse_int: analyses the number to determine its range, hundred to million to billion. and prints the number in words.
int analyse_print(int num[], int m)
{
//These array contain the keywords that would be needed for the conversion.
char *sval[] = { " ", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
char *tval[] = {" " , "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
char *hval[] = {" " ," ","Twenty"," Thirty", "Forty", "Fifty","Sixty", "Seventy", "Eighty", "Ninety"};
char *nval[] = {"Hundred", "Thousand", "Million", "Billion"};

switch(m)
{
//This converts numbers between 1 to 10.
case 1:                        if (num[0] == 0)
{
printf("\nZero.");
}

else
printf("\n%s", sval[num[0]]);

break;

//This converts numbers between 10 and 99.
case 2:                          if ( num[0] == 1)
{
printf("\n%s", tval[num[1]]);
}
else
{
printf("\n%s %s", hval[num[0]],sval[num[1]]);
}

break;

//This converts numbers between 100 and 999.
case 3:
if ( num[1] == 1)
{
printf("\n%s hundred   and %s", sval[num[0]], tval[num[2]]);
}

else
printf("\n%s hundred  and %s %s", sval[num[0]], hval[num[1]], sval[num[2]]);
break;

//This converts between 1000 and 9999.
case 4:                          if ( num[2] == 1)
{
printf("\n%s Thousand %s hundred and %s ", sval[num[0]], sval[num[1]], tval[num[3]]);
}

else
printf("\n%s Thousand %s Hundred and %s %s ", sval[num[0]], sval[num[1]], hval[num[2]], num[3]);

break;

//10,000 and 99,999.....
case 5:
if ( num[0] == 1 && num[3] == 1)
{
printf("\n%s thousand %s hundred and %s.",tval[num[1]], sval[num[2]],tval[num[5]]);
}

else if ( num[0] == 1)
{
printf("\n%s Thousand %s Hundred and %s %s", tval[num[1]], sval[num[2]],nval[0], hval[num[3]], sval[num[4]]);
}

else if ( num[3] == 1)
{
printf("\n%s %s Thousand, %s Hundred and  %s ", hval[num[0]], sval[num[1]], sval[num[2]],tval[num[4]]);
}
else
{
printf("\n%s %s Thousand, %s Hundred and %s %s ", hval[num[0]], sval[num[1]], sval[num[2]],hval[num[3]], sval[num[4]]);
}

break;

//100,000 and 999,999...
case 6:                         if ( num[1] == 1 && num[4] == 1)
{
printf("\n%s Hundred and %s Thousand, %s Hundred and %s ",sval[num[0]], tval[num[2]],sval[num[3]], tval[num[5]]);
}

else if ( num[1] == 1)
{
int j = num[2];
printf("\n%s Hundred %s thousand  %s hundred and %s %s",sval[num[0]], tval[j], sval[num[3]], hval[num[4]], sval[num[5]]);
}

else if ( num[4] == 1)
{
printf("\n%s hundred %s %s thousand, %s hundred and %s ",sval[num[0]], hval[num[1]], sval[num[2]], sval[num[3]],tval[num[5]]);
}

else
{
printf("\n%s hundred and %s %s thousand, %s hundred and %s %s ",sval[num[0]], hval[num[1]], sval[num[2]], sval[num[3]],hval[num[4]], sval[num[5]]);
}

break;

//1,000,000 and 9,999,999.
case 7:                         if ( num[2] == 1 && num[5] == 1 )
{
printf("\n%s million, %s hundred and %s thousand, %s hundred and %s ", sval[num[0]], sval[num[1]], tval[num[3]], sval[num[4]], tval[num[6]]);
}

else if ( num[2] == 1)
{
printf("\n%s million,%s hundred and %s thousand, %s hundred and %s %s",sval[num[0]],sval[num[1]], tval[num[3]], sval[num[4]], hval[num[5]], sval[num[6]]);
}

else if ( num[5] == 1)
{
printf("\n%s %s,%s %s and %s %s %s, %s %s and %s ",sval[num[0]],nval[2],sval[num[1]], nval[0], hval[num[2]], sval[num[3]],nval[1], sval[num[4]],nval[0],tval[num[6]]);
}

else
{
printf("\n%s %s,%s %s and %s %s %s, %s %s and %s %s ",sval[num[0]], nval[2], sval[num[1]], nval[0], hval[num[2]], sval[num[3]],nval[1], sval[num[4]],nval[0],hval[num[5]], sval[num[6]]);
}

break;

//10,000,000 and 99,999,999....
case 8:                         if ( num[0] == 1 && num[3] == 1 && num[6] == 1)
{
printf("\n%s %s, %s %s and %s %s, %s %s and %s", tval[num[1]], nval[2], sval[num[2]], nval[0], tval[num[4]], nval[1], sval[num[5]], nval[0], tval[num[7]]);
}

else if ( num[0] == 1 && num[3])
{
printf("\n%s %s, %s %s and %s %s, %s %s and %s %s", tval[num[1]], nval[2], sval[num[2]], nval[0], tval[num[4]], nval[1], sval[num[5]], nval[0], hval[num[6]], sval[num[7]]);
}

else if ( num[0] == 1 && num[6] == 1)
{
printf("\n%s %s, %s %s and %s %s %s , %s %s and %s", tval[num[1]], nval[2], sval[num[2]], nval[0], hval[num[3]], sval[num[4]],nval[1], sval[num[5]],nval[0], tval[num[7]]);
}

else if ( num[0] == 1)
{
printf("\n%s %s, %s %s and %s %s %s, %s %s and %s %s ", tval[num[1]], nval[2], sval[num[2]], nval[0], hval[num[3]], sval[num[4]],nval[1], sval[num[5]],nval[0], hval[num[6]], sval[num[7]]);
}

else if ( num[3] == 1 && num[6] == 1)
{
printf("\n%s %s %s, %s %s and %s %s, %s %s and %s", hval[num[0]], sval[num[1]], nval[2], hval[num[2]], nval[0], tval[num[4]], nval[1],sval[num[5]], nval[0], tval[num[7]]);
}

else if ( num[3] == 1)
{
printf("\n%s %s %s, %s %s and %s %s, %s %s and %s %s", hval[num[0]], sval[num[1]], nval[2], hval[num[2]], nval[0], tval[num[4]],nval[1],sval[num[5]], nval[0], hval[num[6]], sval[num[7]]);
}

else if ( num[6] == 1)
{
printf("\n%s %s %s, %s %s and %s %s %s, %s %s and %s ",  hval[num[0]], sval[num[1]], nval[2], hval[num[2]], nval[0], hval[num[3]], sval[num[4]], nval[1],sval[num[5]], nval[0], tval[num[7]]);
}

else
printf("\n%s %s %s, %s %s and %s %s %s, %s %s and %s %s ", hval[num[0]], sval[num[1]], nval[2], hval[num[2]], nval[0], hval[num[3]], sval[num[4]], nval[1],sval[num[5]], nval[0], hval[num[6]],sval[num[7]]);

break;

//100,000,000 and 999,999,999......
case 9:                             if ( num[1] == 1 && num[4] == 1 && num[7] == 1)
{
printf("\n %s %s  and %s %s, %s %s and %s %s, %s %s and %s ", sval[num[0]], nval[0], tval[num[2]], nval[2], sval[num[3]],nval[0],tval[num[5]], nval[1], sval[num[6]], nval[0], tval[num[8]]);
}

else if( num[1] == 1 && num[4] == 1)
{
printf("\n%s %s and %s %s, %s %s and %s %s, %s %s and %s %s ", sval[num[0]], nval[0], tval[num[2]], nval[2], sval[num[3]], nval[0], tval[num[5]], nval[1], sval[num[6]], nval[0], hval[num[7]], sval[num[8]]);
}

else if ( num[1] == 1 && num[7] == 1)
{
printf("\n%s %s and %s %s, %s, %s and %s %s %s, %s %s and %s", sval[num[0]], nval[0], tval[num[2]], nval[2], sval[num[3]],nval[0],hval[num[4]], sval[num[5]], nval[1], sval[num[6]], nval[0], tval[num[8]]);
}

else if ( num [1] == 1)
{
printf("\n%s %s and %s %s, %s %s and %s %s %s, %s %s and %s %s", sval[num[0]], nval[0], tval[num[2]], nval[2], sval[num[3]],nval[0], hval[num[4]], sval[num[5]], nval[1], sval[num[6]], nval[0], hval[num[7]], sval[num[8]]);
}

else if ( num[4] == 1 && num[7] == 1)
{
printf("\n %s %s and %s %s %s, %s %s and %s %s, %s %s and %s ", sval[num[0]], nval[0], hval[num[1]], sval[num[2]], nval[2], sval[num[3]],nval[0], tval[num[5]], nval[1],sval[num[6]],nval[0], tval[num[8]]);
}

else if ( num[4] == 1)
{
printf("\n%s %s and %s %s %s , %s %s and %s %s, %s %s and %s %s ", sval[num[0]], nval[0], hval[num[1]], sval[num[2]], nval[2], sval[num[3]], nval[0], tval[num[5]],nval[1], sval[6], nval[0], hval[num[7]], sval[num[8]]);
}

else if ( num [7] == 1)
{
printf("\n%s %s and %s %s %s, %s %s and %s %s %s, %s %s and %s ", sval[num[0]], nval[0], hval[num[1]], sval[num[2]],nval[2], sval[num[3]],nval[0], hval[num[4]], sval[num[5]], nval[1], sval[num[6]], nval[0], tval[num[8]]);
}

else
printf("\n%s %s and %s %s %s, %s %s and %s %s %s, %s %s and %s %s",sval[num[0]], nval[0], hval[num[1]], sval[num[2]],nval[2], sval[num[3]],nval[0], hval[num[4]], sval[num[5]], nval[1], sval[num[6]], nval[0], hval[num[7]], sval[num[8]]);

break;
default:                     fprintf(stderr, "No value entered. Exiting now...");
exit (EXIT_FAILURE);
}

return 0;
}```

2. these thing has been a pain in the ass for about 2 weeks now......

3. Are you just venting, or do you have a question?

Just looking at your code give me a pain, not in my ... well, but still.

4. This code makes baby Jesus cry.

5. Rather than an array of digits, why not just pass a whole number, and discover the magic of /1000 and %1000

6. To do this more programmatically, note that there is a double loop in the problem. The outer loop decomposes the input number by 1000's, while the (unrolled) inner loop decomposes the 100's, 10's, and 1's for each of the power's of 1000. E.g.,

123123123
One Hundred Twenty Three Million One Hundred Twenty Three Thousand One Hundred Twenty Three

The "One Hundred Twenty Three" is the same for billions, millions, thousands, or ones.

So you might try something as indicated in this (untested) pseudocode:
Code:
```// int n is value to print
thousands = 1000000000   // billion
do {
m = n / thousands;
if m > 0:
print hundreds, tens, ones of m
print word based on value of thousands.
n %= thousands;
thousands /= 1000;
}while thousands > 1```

7. hey guys i have modified the code and it works perfectly now... any way thanks @oogabooga for the tip and others....

8. The pseudocode I posted above should have a loop condition of thousands > 0 (it should still execute once more for thousands == 1).

9. ok but i just debugged the read_int() function and it converted successfully any way i'll still try your s its easier.... and a little more complex...thanks @oogobooga....