I wrote a program that will ask for an integer input (1 - 9999) and will convert the inputted integer into its corresponding word format in English.
And I'm trying to modify it so that switch statements will be used instead of if-statements (where it is applicable).
Example 1: Input number: 2481 Output: two thousand four hundred eighty one
Here is my code:
#include<stdio.h>
#include<conio.h>
main()
{
int num,thousands,hundreds,tens,ones;
printf("Enter number (1-9999): ");
scanf("%d",&num);
//&& - check if within the range
//|| - check if outside of the range
if (num < 1 || num > 9999)
printf("Invalid number.");
else
//if (num > 0 && num < 10000)
{
thousands = num / 1000;
hundreds = num % 1000 / 100;
tens = num % 1000 % 100 / 10;
ones = num % 1000 % 100 % 10;
//if (num / 1000 == 1)
if(thousands == 1)
printf("one thousand ");
if(thousands == 2)
printf("two thousand ");
if(thousands == 3)
printf("three thousand ");
if(thousands == 4)
printf("four thousand ");
if(thousands == 5)
printf("five thousand ");
if(thousands == 6)
printf("six thousand ");
if(thousands == 7)
printf("seven thousand ");
if(thousands == 8)
printf("eight thousand ");
if(thousands == 9)
printf("nine thousand ");
//if (num % 1000 / 100 == 1)
if(hundreds == 1)
printf("one hundred ");
if(hundreds == 2)
printf("two hundred ");
if(hundreds == 3)
printf("three hundred ");
if(hundreds == 4)
printf("four hundred ");
if(hundreds == 5)
printf("five hundred ");
if(hundreds == 6)
printf("six hundred ");
if(hundreds == 7)
printf("seven hundred ");
if(hundreds == 8)
printf("eight hundred ");
if(hundreds == 9)
printf("nine hundred ");
//if (num % 1000 % 100 / 10 == 1)
if(tens == 1)
{
//if (num % 1000 % 100 % 10 == 0)
if(ones == 0)
printf("ten ");
if(ones == 1)
printf("eleven ");
if(ones == 2)
printf("twelve ");
if(ones == 3)
printf("thirteen ");
if(ones == 4)
printf("fourteen ");
if(ones == 5)
printf("fifteen ");
if(ones == 6)
printf("sixteen ");
if(ones == 7)
printf("seventeen ");
if(ones == 8)
printf("eighteen ");
if(ones == 9)
printf("nineteen ");
}
if(tens == 2)
printf("twenty ");
if(tens == 3)
printf("thirty ");
if(tens == 4)
printf("forty ");
if(tens == 5)
printf("fifty ");
if(tens == 6)
printf("sixty ");
if(tens == 7)
printf("seventy ");
if(tens == 8)
printf("eighty ");
if(tens == 9)
printf("ninety ");
if (tens != 1)
{
if(ones == 1)
printf("one ");
if(ones == 2)
printf("two ");
if(ones == 3)
printf("three ");
if(ones == 4)
printf("four ");
if(ones == 5)
printf("five ");
if(ones == 6)
printf("six ");
if(ones == 7)
printf("seven ");
if(ones == 8)
printf("eight ");
if(ones == 9)
printf("nine ");
}
}
//else
//printf("Invalid number.");
getch();
}
However, when I tried substituting the if statements into switch statements it displays a blank. I'm stuck at the thousands place. I'm trying to fix it first before getting to the hundreds, tenths, and ones. Here is what I tried:
#include<stdio.h>
#include<conio.h>
main()
{
int num,thousands,hundreds,tens,ones;
printf("Enter number (1-9999): ");
scanf("%d",&num);
if (num < 1 || num > 9999)
printf("Invalid number.");
else
{
thousands = num / 1000;
switch (thousands)
{
case '1': printf("one thousand ");
break;
case '2': printf("two thousand ");
break;
case '3': printf("three thousand ");
break;
case '4': printf("four thousand ");
break;
case '5': printf("five thousand ");
break;
case '6': printf("six thousand ");
break;
case '7': printf("seven thousand ");
break;
case '8': printf("eight thousand ");
break;
case '9': printf("nine thousand ");
break;
}
}
}