# Thread: Need to create an integer-to-word program

1. ## Need to create an integer-to-word program

"Write a program that takes a two-digit integer (-99 to +99) as an input and outputs the number in words. If the input is outside this range, the program should print that it is outside the range."

To be honest guys, I'm pretty much clueless on how to do this. I know I need to use the if and switch statements, but anything beyond that is over my head. Can anyone help me get started?

see which word you are using
and create a static array containing all these words
then start to write conditions how to select correct word from your "dictionary"

3. So it looks like I'm gonna need

one - nineteen (manually)
twenty, thirty, forty, fifty, sixty, seventy, eighty, and ninety

And somehow I need to have the program identify which inputs are in which places. I.e. if it sees 09, it needs to be able to see that the first digit is blank, and the 9, being the second digit, is output as "nine" and not "ninety"

4. I actually have one such implementation at hand as we speak.
I created a table for the numbers 0-19. And a table for the numbers 20 to 90.
I would then go for various tests, and build up a string with strcat() during the process.
E.g.
Code:
```char result[1024];
memset(result, 0, 1024);

if(x>=0 && x<=9)
{
strcat(result, num0_20[x]);
} else
if(x>=10 && x<=19)
{
strcat(result, num0_20[x]);
}```
Figure out how to do the rest.

5. I dont think we've learned all the things your program employs

6. It was not supposed to be a solution but a hint to a solution.
Code:
`char *num0_20[] = {"zero","one",...};`
is the missing declaration that confuses you.
The rest of the answer is trivial, and has to do with you reading the number and converting it to true number, and using it to select which string to concatenate.
Think for a moment how to use the above code if x is 39?
How about finding how many times 10 divides x and choosing an appropriate prefix, eg. "thirty".
How would you do that?

7. Code:
```/*
compile and run:
gcc -lm this_program_code.c
./a.out

I tested all of the numbers with this bash command:
for i in `seq -99 100` ; do ./a.out \$i ; done

Disclaimer:
I am exempt from using C comments, because I like you.
*/
#include <stdio.h>
#include <math.h>

#define SHORTCUT_BIATCH \
switch(second_digit){\
case 1:\
puts("one");break;\
case 2:\
puts("two");break;\
case 3:\
puts("three");break;\
case 4:\
puts("four");break;\
case 5:\
puts("five");break;\
case 6:\
puts("six");break;\
case 7:\
puts("seven");break;\
case 8:\
puts("eight");break;\
case 9:\
puts("nine");break;\
}

// Let's get this party started
int main(int argc, char **argv){
int number_from_user;
int first_digit, second_digit;
double temporary_number;

// ask for a number -- from command line or command-line input
if(argc == 2)
sscanf(argv[1], "%d", &number_from_user);
else{
printf("Just give me a freakin' number--  ");
fscanf(stdin, "%d", &number_from_user);
}

// is the number outside of the range?
if(number_from_user > 99 || number_from_user < -99){
puts("Error: OUT OF RANGE -- hacker alert!!! lolllly pop");
return 0;
}

printf("%d: ", number_from_user);

// is the number negative?
if(number_from_user < 0){
printf("negative ");
number_from_user = -1 * number_from_user;
}

// get the first digit  Xx (the big X)
// (for example, if 34 was the user's input, '3' would be the first digit)
first_digit = (int) floor(number_from_user / 10.0);
//printf("First digit: %d\n", first_digit);  //-- debugging bull........

// get the second digit xX (the big X)
second_digit = number_from_user - first_digit * 10;
//printf("Second digit: %d\n", second_digit);  // -- debugging crap
//puts("---------------");

// output the number in words
// based on the first and second digits of the numbers they entered.
switch(first_digit){
case 0:
if(second_digit == 0)
puts("zero");
SHORTCUT_BIATCH
break;
case 1:
switch(second_digit){
case 0:
puts("ten");break;
case 1:
puts("eleven");break;
case 2:
puts("twelve");break;
case 3:
puts("thirteen");break;
case 4:
puts("fourteen");break;
case 5:
puts("fiveteen");break;
case 6:
puts("sixteen");break;
case 7:
puts("seventeen");break;
case 8:
puts("eighteen");break;
case 9:
puts("nineteen");break;
}
break;
case 2:
printf("twenty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 3:
printf("thirty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 4:
printf("fourty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 5:
printf("fiftey ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 6:
printf("sixty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 7:
printf("seventy ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 8:
printf("eighty ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
case 9:
printf("ninety ");
if(second_digit == 0)puts("");
SHORTCUT_BIATCH
break;
}

return 0;  // successorz :D  yay
}```

8. This reminds me of some of the slowest sorting algorithms iMalc had on his page, only this one goes for the longest unnecessary solution candidate :P No offense just joking....

9. Code:
```  switch(first_digit){
case 0:
if(second_digit == 0)
puts("zero");
SHORTCUT_BIATCH
break;
case 1:```
lmao, oh by the way after posting i still laughed out loud, good #define