Complete #6 from the Chapter 6 exercises, where the user will input an integer a
ID: 3543808 • Letter: C
Question
Complete #6 from the Chapter 6 exercises, where the user will input an integer and you will output the english words for the digits ("zero" through "nine"). For example, if the user types in the 3-digit number 498 (as ONE integer), then the output would be:
four nine eight
You may choose which grade you would like to work toward, assuming that your program runs properly, includes appropriate documentation and follows layout guidelines (3 spaces each indent, braces lined up, etc.). All programs should incorporate a SWITCH statement.
Grade of "C" (75%): You will specify how many digits the integer should have when input by the user (at least 3) and assume the user is intelligent enough to follow directions.
Grade of "B" (85%): You will specify how many digits the integer should have when input by the user (at least 3) but will check to make sure that they followed directions, giving them a chance to re-input their integer if their integer has the improper number of digits. The integer may be positive or negative, so make sure that you print "negative" in front of the number words if it's negative.
Explanation / Answer
#include <stdio.h>
int main()
{
int n;
int divisor = 100;
printf("Enter a 3-digit number: ");
scanf("%d", &n);
while (n > 999 || n < -999)
{
printf("Invalid length. Please re-enter a 3-digit number: ");
scanf("%d", &n);
}
if (n < 0)
{
printf("negative ");
n *= -1;
}
while (divisor)
{
switch (n/divisor)
{
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;
default: break;
}
n %= divisor;
divisor /= 10;
}
return 0;
}
//with proper indentation: http://ideone.com/COfNmB
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.