Chapter 3 (Loops and Decisions) - Exercises

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Assume that you want to generate a table of multiples of any given number. Write a program that allows the user to enter the number and then generates the table, formatting it into 10 columns and 20 lines. Interaction with the program should look like this (only the first three lines are shown): Enter a number: 7 7 14 21 28 35 42 49 56 63 70 // 77 84 91 98 105 112 119 126 133 140 // 147 154 161 168 175 182 189 196 203 210

// ex3_1.cpp // displays multiples of a number #include <iostream> #include <iomanip> //for setw() using namespace std; int main() { unsigned long n; //number cout << "\nEnter a number: "; cin >> n; //get number for(int j=1; j<=200; j++) //loop from 1 to 200 { cout << setw(5) << j*n << " "; //print multiple of n if( j%10 == 0 ) //every 10 numbers, cout << endl; //start new line } return 0; }

Write a temperature-conversion program that gives the user the option of converting Fahrenheit to Celsius or Celsius to Fahrenheit. Then carry out the conversion. Use floating-point numbers. Interaction with the program might look like this: Type 1 to convert Fahrenheit to Celsius, 2 to convert Celsius to Fahrenheit: 1 Enter temperature in Fahrenheit: 70 In Celsius that's 21.111111

// ex3_2.cpp // converts fahrenheit to centigrad, or // centigrad to fahrenheit #include <iostream> using namespace std; int main() { int response; double temper; cout << "\nType 1 to convert fahrenheit to celsius," << "\n 2 to convert celsius to fahrenheit: "; cin >> response; if( response == 1 ) { cout << "Enter temperature in fahrenheit: "; cin >> temper; cout << "In celsius that's " << 5.0/9.0*(temper-32.0); } else { cout << "Enter temperature in celsius: "; cin >> temper; cout << "In fahrenheit that's " << 9.0/5.0*temper + 32.0; } cout << endl; return 0; }

Operators such as >>, which read input from the keyboard, must be able to convert a series of digits into a number. Write a program that does the same thing. It should allow the user to type up to six digits, and then display the resulting number as a type long integer. The digits should be read individually, as characters, using getche(). Constructing the number involves multiplying the existing value by 10 and then adding the new digit. (Hint: Subtract 48 or '0' to go from ASCII to a numerical digit.) Here's some sample interaction: Enter a number: 123456 Number is: 123456

// ex3_3.cpp // makes a number out of digits #include <iostream> using namespace std; #include <conio.h> //for getche() int main() { char ch; unsigned long total = 0; //this holds the number cout << "\nEnter a number: "; while( (ch=getche()) != '\r' ) //quit on Enter total = total*10 + ch-'0'; //add digit to total*10 cout << "\nNumber is: " << total << endl; return 0; }

Create the equivalent of a four-function calculator. The program should ask the user to enter a number, an operator, and another number. (Use floating point.) It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result. When it finishes the calculation, the program should ask whether the user wants to do another calculation. The response can be 'y' or 'n'. Some sample interaction with the program might look like this: Enter first number, operator, second number: 10 / 3 Answer = 3.333333 Do another (y/n)? y Enter first number, operator, second number: 12 + 100 Answer = 112 Do another (y/n)? n

// ex3_4.cpp // models four-function calculator #include <iostream> using namespace std; int main() { double n1, n2, ans; char oper, ch; do { cout << "\nEnter first number, operator, second number: "; cin >> n1 >> oper >> n2; switch(oper) { case '+': ans = n1 + n2; break; case '-': ans = n1 - n2; break; case '*': ans = n1 * n2; break; case '/': ans = n1 / n2; break; default: ans = 0; } cout << "Answer = " << ans; cout << "\nDo another (Enter 'y' or 'n')? "; cin >> ch; } while( ch != 'n' ); return 0; }


Kaugnay na mga set ng pag-aaral

Finance Ch 15 - Capital Structure

View Set

Pharm Test Specific Neurodegenerative Disorders

View Set

Chapter 2 (formulating hypotheses and research questions)

View Set