Chapter2 Exercises
Chapter2 Exercises
* Exercise 2.1:
Restaurant Bill
Write a program that computes the tax and tip on a restaurant bill for a patron with a
$88.67 meal cost. The tax is 6.75 percent of the meal cost and the tip is 20 percent of the
meal cost after adding the tax. Display the total cost, tax amount, tip amount, and total
bill on the screen.
Solution 2.1: Your code
* Exercise 2.2:
Future Ocean Levels
During the past decade ocean levels have been rising faster than in the past, an average of
approximately 3.1 millimeters per year. Write a program that computes the ocean levels
expected to rise in the next 20 years if they continue rising at this rate. Display the result
in both centimeters and inches measures.
Solution 2.2: Your code
* Exercise 2.3:
Write a program to swap values of two variables.
Solution: Your code
* Exercise 2.4:
Write a program that prompts a user for two integers and performs all arithmetic
operations (addition, subtraction, multiplication and division).
Solution: Your code
* Exercise 2.5:
Write a program that prompts a user for the radius of a circle and computes the perimeter
and the area of the circle.
Test Cases:
Input Expected Output
5 Perimeter: 31.42, Area: 78.54
-5 Radius must >0
Solution: Your code
* Exercise 2.6:
Write a program that prompts a user for height and width of a rectangle and then
computes the perimeter and the area of the rectangle.
Test Cases:
Input Expected Output
5 10 Perimeter: 30.00, Area: 50.00
-4 20/20 -30 Height and Width must >0
* Exercise 2.7:
Write a program that prompts a user for an integer and computes the weeks and days
from the entered number.
Test Cases:
Input Expected Output
1329 Weeks: 189, Days: 6
-12 Number must >0
5 Weeks: 0, Days: 5
* Exercise 2.8:
Write a program that prompts a user for hours and minutes and then calculates the
number of minutes from the entered values.
Test Cases:
Input Expected Output
5 37 Total minutes: 337
-5 37/5 -37/ -5 -37 Hour and minute must >= 0
0 37 Total minutes: 37
20 Total minutes: 120