Chapter 3 Exercises¶
Write down what you think each of these would print, then use the active code window to check your results:
9 * 5
2 / 5
5 % 2
9 % 5
6 % 6
2 % 7
3 / 0
9 * 5 = 45
2 / 5 = 0.4
5 % 2 = 1
9 % 5 = 4
6 % 6 = 0
2 % 7 = 2
error - you can’t divide by zero
Insert the correct operators in place of the
#s so each line prints
==checks for equality.
Add a set of parentheses to the expression
x = 6 * 1 - 2so that the code below prints -6 instead of 4.
Add the parentheses around the
1 - 2as shown below.
Add parentheses to
x = 12 * 2 - 3 + 4 * 2so that it prints -4 instead of 29.
Add the parentheses around the
2 - 3and the
4 * 2as shown below.
Complete the code on lines 3 and 5 below to print the cost of a car trip of 500 miles when the car gets 26 miles per gallon and gas costs 3.45 a gallon. It should print 66.3461538462.
miles / milesPerGallon. Calculate
numGallons * pricePerGallon.
If Sunday is represented by 1, Monday by 2, Tuesday by 3, etc., and today is Sunday, complete the code on line 4 (with a math expression) to show what day it will be 82 days from today (it should print 6 which represents Friday)
Complete the code on lines 4 and 5 to print how many miles you can drive on $25 if your car gets 40 miles per gallon and the price of gas is $3.65 a gallon. It should print 273.97260274.
funds / pricePerGallon. Calculate
milesPerGallon * numGallons.
Fix the syntax errors.
The variable should be on the left and the value should be assigned should be on the right, and there should be no spaces in the variable name.
Complete the code on lines 3 and 7 to print the final cost for an item that is priced $68, but is 40% off the original price and you have a coupon to take an additional 20% of the sale price. It should print 32.64.
price * amountOff. Calculate
salePrice - couponReduction.
Fix the syntax and semantic errors so that the answer is 1 instead of 3.5
The variable should be on the left and the value should be assigned should be on the right. Also, change the
Finish the code on lines 4 and 5 to print how many wings you can buy if you have 5 people and they each can spend $4 a person and the wings are $0.50 a wing. It should print 40.0.
numPeople * amountPerPerson. Calculate
total / price.
It is currently 10:00, complete the code to tell what time it is going to be in 123 hours (12-hour time, not 24-hour time) (Answer should be 1)
The time on the clock can be represented as a modulus of 12.
Finish the code on lines 2 and 3 in the code below to print how many hours and minutes you have been waiting when you have been waiting a total of 270 minutes. Remember that there are 60 minutes in an hour. It should print 4.0 and then 30.
totalMinutes % 60. Calculate
(totalMinutes - numMinutes) / 60.
You’re buying groceries and your sub-total is $73, but you have to pay 7% tax. Complete the code to find your total price. Total should be 78.11
The total should be the subTotal plus the subtotal times the tax.
Fix the syntax errors in the code below so that it calculates and prints the number of hours you will need to work if you earn $8 an hour and want to earn $100. It should print 12.5.
Change the first line to
payPerHour = 8. Change the third line to
numHours = amount / payPerHour.
Complete the code to show how many minutes are in 1.3 days and how many seconds are in 1.3 days. It should print 1872.0 and 112320.0
Number of minutes is the number of hours times 60 and seconds is the number of minutes times number of seconds.
Finish lines 5 and 6 in the code below to print how many apples you can buy when apples cost 0.60 and you want to get 3 pears and they cost $1.2 each and you have $8.00. It should print 7.33333333333. Since you can’t buy 7.333 apples can you also figure out how to make it print just 7?
funds - (pricePerPear * numPears). Calculate
fundsAfterPears / pricePerApple. You can throw away the fractional part using
A car consumes fuel at a rate of 23 mpg. Someone fills the car up with 15 gallons of gas and drives 112 miles. Fill in the code to determine how many more gallons are left. The answer should be 10.13043478260869
Gas consumed is distance divided by gas rate and gas remaining is the amount of gas minus the gas consumed.
Write the code to calculate and print how many miles you can drive if your car holds 10 gallons and you have a quarter of a tank left and your car gets 32 miles per gallon. It should print 80.
Create variables to hold each value. Calculate
tankCapacity * 0.25. Calculate
numGallons * milesPerGallon. Be sure to print the result.
A bullet is travelling 25 m/s. Write code to determine how many seconds it will take to travel 111 m. (It should be 4.44 seconds)