# Thread: program that reads a number between 1 and 999 and spells it in english

1. ## program that reads a number between 1 and 999 and spells it in english

This is my second program and I'm stuck, i don't know if I'm doing it right, so far i did the hundreds, and tenths, i don't know how to do the Ones, and the 11, 12,......
Also please let me know of those little mistakes too like the brackets and signs.

Write a program that reads a number between 1 and 999 from user and spells out it in English.

For example:
If number is 453, your program prints out “Four hundred fifty three”
If number is 37, your program prints out “Thirty seven”
If number is 214, your program prints out “Two hundred fourteen”
while(1) {
printf(“Enter a number between 1-999 (enter 0 to exit): “);
scanf(“%d”, &number);
if (number==0) break;
if (number > 999 || number < 1) continue;

Code:
```#include<stdio.h>
#include<math.h>

int main(void)
{
while(1) {
printf("Enter a number between 1-999 (enter 0 to exit): ");
scanf("%d", &number);
if (number==0) break;
if (number > 999 || number < 1) continue
a=number/100;
b=number/10;

switch(a){
case 1:
printf("One Hundred");
break;
case 2:
case 1:
printf("Two Hundred");
break;
case 3:
printf("Three Hundred");
break;
case 4:
printf("Four Hundred");
break;
case 5:
printf("Five Hundred");
break;
case 6:
printf("Six Hundred");
break;
case 7:
printf("Seven Hundred");
break;
case 8:
printf("Eight Hundred");
break;
case 9:
printf("Nine Hundred");
break;

switch(b){
case 1:
printf("Ten");
break;
case 2:
printf("Twenty");
break;
case 3:
printf("Thirty");
break;
case 4:
printf("Forty");
break;
case 5:
printf("Fifty");
break;
case 6:
printf("Sixty");
break;
case 7:
printf("Seventy");
break;
case 8:
printf("Eighty");
break;
case 9:
printf("Ninety");
break;

return 0;

}```

2. Same approach as you've got so far would seem to work.

3. Code:
```#include<stdio.h>
#include<math.h>

int main() {
int hundreds = 0;
int tens = 0;
int ones = 0;
int number; //input

while(1) {
printf("Enter a number between 1-999 (enter 0 to exit): ");
scanf("%d", &number);

if (number==0) break;

hundreds = number / 100;
tens = (number - (hundreds * 100)) / 10;
ones = number - (hundreds * 100) - (tens * 10);

if (hundreds > 0) {
if      (hundreds == 9) printf("Nine");
else if (hundreds == 8) printf("Eight");
else if (hundreds == 7) printf("Seven");
else if (hundreds == 6) printf("Six");
else if (hundreds == 5) printf("Five");
else if (hundreds == 4) printf("Four");
else if (hundreds == 3) printf("Three");
else if (hundreds == 2) printf("Two");
else                    printf("One");

printf(" Hundred ");
//if (tens == 0 && ones == 0) printf("\n");
}

if (tens != 1) {  // we have a special case (teens)
if      (tens == 9) printf("Ninety ");
else if (tens == 8) printf("Eighty ");
else if (tens == 7) printf("Seventy ");
else if (tens == 6) printf("Sixty ");
else if (tens == 5) printf("Fifty ");
else if (tens == 4) printf("Forty ");
else if (tens == 3) printf("Thirty ");
else if (tens == 2) printf("Twenty ");

if (ones > 0) {
if      (ones == 9) printf("Nine\n");
else if (ones == 8) printf("Eight\n");
else if (ones == 7) printf("Seven\n");
else if (ones == 6) printf("Six\n");
else if (ones == 5) printf("Five\n");
else if (ones == 4) printf("Four\n");
else if (ones == 3) printf("Three\n");
else if (ones == 2) printf("Two\n");
else                printf("One\n");
} else {
printf("\n");
}
} else {
if (ones > 0) {
if      (ones == 9) printf("Nineteen\n");
else if (ones == 8) printf("Eightteen\n");
else if (ones == 7) printf("Seventeen\n");
else if (ones == 6) printf("Sixteen\n");
else if (ones == 5) printf("Fifteen\n");
else if (ones == 4) printf("Fourteen\n");
else if (ones == 3) printf("Thirten\n");
else if (ones == 2) printf("Twelve\n");
else                printf("Eleven\n");
} else {
printf("Ten\n");
}
}
}

return 0;
}```

4. I used if/else ifs but your switches work ok... its just the math part of yours thats iffy. but you were on the right track

5. Ok, I was bored so I decided to code it myself. It's not perfect. For instance, the last character it prints is always a space. And there might still be bugs, I haven't tested much. But at least it shows one of the better ways to do it.

Code:
```#include <stdio.h>
#include <stdlib.h>

static struct {
int value;
const char *name;
} number_scales[] = {
{ 1000000, "million" },
{ 1000, "thousand" },
{ 100, "hundred" }
};

void print_number(int num)
{
unsigned int i;

if(num < 0) {
printf("minus ");
num = -num;
}

for(i = 0; i < sizeof(number_scales) / sizeof(*number_scales); i++) {
int scale_value = num / number_scales[i].value;
if(!scale_value)
continue;

print_number(scale_value);
printf("%s ", number_scales[i].name);
num %= number_scales[i].value;
if(num == 0)
return;
}

if(num >= 20) {
switch(num / 10) {
case 2: printf("twenty "); break;
case 3: printf("thirty "); break;
case 4: printf("fourty "); break;
case 5: printf("fifty "); break;
case 6: printf("sixty "); break;
case 7: printf("seventy "); break;
case 8: printf("eighty "); break;
case 9: printf("ninety "); break;
default: break;
}

num %= 10;
if(num == 0)
return;
}

switch(num) {
case 0: printf("zero "); break;
case 1: printf("one "); break;
case 2: printf("two "); break;
case 3: printf("three "); break;
case 4: printf("four "); break;
case 5: printf("five "); break;
case 6: printf("six "); break;
case 7: printf("seven "); break;
case 8: printf("eight "); break;
case 9: printf("nine "); break;
case 10: printf("ten "); break;
case 11: printf("eleven "); break;
case 12: printf("twelve "); break;
case 13: printf("thirteen "); break;
case 14: printf("fourteen "); break;
case 15: printf("fifteen "); break;
case 16: printf("sixteen "); break;
case 17: printf("seventeen "); break;
case 18: printf("eighteen "); break;
case 19: printf("nineteen "); break;
default: break;
}
}

int main(int argc, char **argv)
{
int num = argc > 1 ? atoi(argv[1]) : 0;
print_number(num);
printf("\n");
return 0;
}```

6. Now given a number N, print out the Nth number (from 0) which does not contain the letter 'e' in its spelling.

7. Code:
```	for(i = 0; i < sizeof(number_scales) / sizeof(*number_scales); i++) {
int scale_value = num / number_scales[i].value;
if(!scale_value)
continue;

print_number(scale_value);
printf("%s ", number_scales[i].name);
num %= number_scales[i].value;
if(num == 0)
return;
}```
If you invert the condtion in red, you can avoid the "continue", and wrap all the remaining code in a block. Makes the code easier to read and follow.

Instead of using switch, why not use a table indexed by the tens and units?

The last:
Code:
`	default: break;`
should probably be a
Code:
`printf("We shouldn't get here: num = %d\n", num);`
statement.

--
Mats