Programming - Ch 3 Exercise Solutions
Modified July 20, 2009.
- Exercise 4:
Write a program that prompts the user to enter two integer values.
Store these values in int variables named val1 and val2.
Write your program to determine the smallest, largest, sum, difference, product, and ratio of these values and report them to the user.
Solution code.
- Exercise 6:
- Write a program that prompts the user to enter 3 integer values, and then outputs the values in numerical sequence separated by commas.
So: if the user enters the values 10 4 6, the output should be 4, 6, 10. If two values are the same, they should just be ordered together.
So, the input 4 5 4 should give 4, 4, 5.
Solution code.
- Exercise 8:
Write a program to test an integer value to determine if it is odd or even.
As always, make sure your output is clear and complete.
In other words, don't just output "yes" or "no".
Your output should stand alone, like ``The value 4 is an even number.''
Hint: see the Remainder (modulo) operator in 2.4.
Solution code.
- Exercise 9:
Write a program that converts spelled out numbers such as "zero" and "two" into digits, such as 0 and 2.
When the user inputs a number print out the corresponding digit.
Do it for the values 0,1,2,3, and 4 and write out "not a number I know" if the user enters something that doesn't correspond, such as ``stupid computer!''
Solution code.
- Exercise 10:
Write a program that takes an operation followed by two operands and outputs the result. For example:
Read the operation into a string called operation and use an if-statement to figure out which operation the user wants.
For example, if (operation=="+"). Read the operands into variables of type double.
Implement this for operations called +, -, *, /, plus, minus, mul, and div with their obvious meanings.
Solution code.