There's this exercise I'm trying to figure out. The assignment asks to convert a two-digit number in words, the output should be something like this :
Enter a two-digit number:45
You entered the number forty-five.
I'm still a total beginner to programming. I'm at this chapter in this C programming book, in the exercise section about the switch
and if
statements. The exercise suggests to use two switch
statements, One for the tens and the other one for units, but numbers within 11
and 19
require special treatment.
The problem is that I'm trying to figure out what should I do for numbers between 11
and 19
, I was thinking to use the if
statement but then the second switch
function would include in the output and it would turn into something like You've entered the number eleven one.
This is the program I've been writing so far (incomplete):
int digits;
printf("Enter a two-digit number:");
scanf("%d", &digits);
printf("You entered the number ");
switch (digits / 10) {
case 20:
printf("twenty-");break;
case 30:
printf("thirty-");break;
case 40:
printf("forty-");break;
case 50:
printf("fifty-");break;
case 60:
printf("sixty-");break;
case 70:
printf("seventy-");break;
case 80:
printf("eighty-");break;
case 90:
printf("ninety-");break;
}
switch (digits % 10) {
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;
}
return 0;
Aucun commentaire:
Enregistrer un commentaire