5 - Chapter 5 - Loops - Files
5 - Chapter 5 - Loops - Files
1. Sum of Numbers
Write a program that asks the user for a positive integer value. The program should use a loop to get the
sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will
find the sum of 1, 2, 3, 4, … 50.
3. Ocean Levels
Assuming the ocean’s level is currently rising at about 1.5 millimeters per year, write a program that
displays a table showing the number of millimeters that the ocean will have risen each year for the next
25 years.
4. Distance Traveled
The distance a vehicle travels can be calculated as follows:
distance = speed * time
For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles.
Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it
has traveled. The program should then use a loop to display the distance the vehicle has traveled for each
hour of that time period. Here is an example of the output:
Input Validation: Do not accept a negative number for speed and do not accept any value less than 1 for
time traveled.
Write a program that generates a random number and asks the user to guess what the number is. If the
user’s guess is higher than the random number, the program should display “Too high, try again.”
If the user’s guess is lower than the random number, the program should display “Too low, try
again.” The program should use a loop that repeats until the user correctly guesses the random number.
b.
Enhance the program that you wrote for problem a above so it keeps a count of the number of guesses
that the user makes. When the user correctly guesses the random number, the program should display
the number of guesses.
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
Pattern A Pattern B
+ ++++++++++
++ +++++++++
+++ ++++++++
++++ +++++++
+++++ ++++++
++++++ +++++
+++++++ ++++
++++++++ +++
+++++++++ ++
++++++++++ +