Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

programming C++ Exercise 3.3.1. The program in Example 3.3 (the number-printing

ID: 3861315 • Letter: P

Question

programming C++

Exercise 3.3.1. The program in Example 3.3 (the number-printing program) is more useful if it allows repeated use rather than requiring you to restart it every time. (Actually that’s true of most programs.) Therefore, place the bulk of the program in a do-while loop that repeats until the user enters 0, at which point it exits.

Exercise 3.3.2. Revise the program so that it can handle numbers in the range 0 to 9. This should be easy.

Exercise 3.3.3. Revise the program so that it can handle numbers in the range 11 to 19. This will require a good deal more work than Exercise 3.3.2 does, because you have to account for all the “teen” words.

and statement is itself a state defau labe example case case case case Case cout "Char. Is break Example 3.3, Print a Number Although computers simple num for presentation to humans. The most sophist erized phone which change numerical we're not going to do anything quite that ten equivalent: printing out numbers in a logic is the same as that used for phone natural and the following application istake print it out in English- example printing printnum.cpp include ciostre amo sing name space std; int mai no cout "Enter a number from 20 to n 10; int tens digits n N 10 fnt units digits The switch-case Statement switch(tens digits) t case 2: cout twenty break case 3 cout thirty break case 4 cout forty break case 5: cout "fifty break case 6: cout Sixty break case 7: cout seventy break case 8 cout eighty break case 9: cout "ninety break switch (units digits) f case 1: cout "one" endi break case 2: cout "two" endl break case 3: cout three endl break case 4: cout "four" endl break. case 5: ut "five" endl break case 6: cout six endl; break. case 7: cout Seven endl break case 8: cout eight" endl break case 9: cout nine endl; break How It Works If you've programmed before in another computer language, you might object that this example could be made much more efficient with something called an "array." That's absolutely true, and I'll get to that efficiency improvement in Chapter 6. Arrays: All in a Row-- But assume for now that switch case is the best have. To understand how this program worksit's necessary to review the division and remainder operators. The division operator, when applied to two inte gers, produces an integer result, rounded down. So, for example, let's suppose the user enters the number 49. The first thing the program doesisextract the tens digit as follows: 49 /10 If the program were, instead, working with floating point data las in the expression 49 01 100), the answer would be 49, which, if becomes 50. But only integers are involved here so the result is rounded down to 4

Explanation / Answer

#include <iostream>

using namespace std;

int main(){
int n = 0;

while(true){
cout << "Enter a number from 20 to 99: ";
cin >> n;
if(n == 0) break;
int hundreds_digit = n / 100;
int tens_digit = n / 10 - hundreds_digit * 10;
int units_digit = n % 10;

switch(hundreds_digit){
case 1: cout << "one hundred "; break;
case 2: cout << "two hundred "; break;
case 3: cout << "three hundred "; break;
case 4: cout << "four hundred "; break;
case 5: cout << "five hundred "; break;
case 6: cout << "six hundred "; break;
case 7: cout << "seven hundred "; break;
case 8: cout << "eight hundred "; break;
case 9: cout << "nine hundred "; break;
}


switch(tens_digit){
case 1:
switch(units_digit){
case 0: cout << "ten "; break;
case 1: cout << "eleven "; break;
case 2: cout << "twelve "; break;
case 3: cout << "thirteen"; break;
case 4: cout << "fourteen "; break;
case 5: cout << "fifteen "; break;
case 6: cout << "sixteen "; break;
case 7: cout << "seventeen "; break;
case 8: cout << "eighteen "; break;
case 9: cout << "nineteen "; break;
}
break;
case 2: cout << "twenty "; break;
case 3: cout << "thirty "; break;
case 4: cout << "forty "; break;
case 5: cout << "fifty "; break;
case 6: cout << "sixty "; break;
case 7: cout << "seventy "; break;
case 8: cout << "eighty "; break;
case 9: cout << "ninety "; break;
}

if(tens_digit != 1){
switch(units_digit){
case 1: cout << "one "; break;
case 2: cout << "two "; break;
case 3: cout << "three"; break;
case 4: cout << "four "; break;
case 5: cout << "five "; break;
case 6: cout << "six "; break;
case 7: cout << "seven "; break;
case 8: cout << "eight "; break;
case 9: cout << "nine "; break;
}
}
cout << endl;
}
}