/* Name: ex5-11.c Purpose: Display a two-digit number in English. Author: Bill Slough Note: The code is a bit tedious, though straightforward. */ #include void displayInEnglish(int n); int numberOfDigits(int n); int main() { int yourNumber; /* Get the input value */ printf("Enter a two-digit number: "); scanf("%d", &yourNumber); if (yourNumber < 0 || numberOfDigits(yourNumber) != 2) { printf("A positive two-digit number was expected.\n"); return 1; } else { printf("You entered the number "); displayInEnglish(yourNumber); printf(".\n"); } return 0; } void displayInEnglish(int n) { /* Display a two-digit number in English Precondition: n is a two-digit integer Result: The appropriate English phrase corresponding to n is output. */ if (n < 20) { /* values 10 through 19 are treated as a special case */ switch (n % 10) { case 0: printf("ten"); break; case 1: printf("eleven"); break; case 2: printf("twelve"); break; case 3: printf("thirteen"); break; case 4: printf("fourteen"); break; case 5: printf("fifteen"); break; case 6: printf("sixteen"); break; case 7: printf("seventeen"); break; case 8: printf("eighteen"); break; case 9: printf("nineteen"); break; } return; } /* for 20 through 99, output the tens digit value... */ switch (n / 10) { 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; } /* ...followed by the value of the ones digit */ switch (n % 10) { case 0: 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; } return; } int numberOfDigits(int n) { /* How many digits does a given integer have? Precondition: n is a non-negative integer Result: returns the number of digits in n */ int digits = 0; do { n /= 10; /* remove the rightmost digit */ digits++; /* count it */ } while (n > 0); return digits; }