24.3k views
0 votes
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: Hour Distance Traveled --------------------------------

1. 40
2. 80
3. 120

1 Answer

5 votes

Final answer:

This program requests the user to input a vehicle's speed and the amount of time it has traveled, and then calculates and displays the distance traveled each hour using a loop. It demonstrates the concept of average speed in a programming scenario.

Step-by-step explanation:

The task here involves writing a simple program that calculates the distance traveled by a vehicle using the formula distance = speed × time. A user provides the speed of the vehicle in miles per hour and the time it has been traveling for, and the program must output the distance for each of the hours traveled. This demonstrates the practical application of average speed calculations in a programming context.

  1. Ask the user for the vehicle's speed (in miles per hour).
  2. Ask the user for the hours traveled.
  3. Use a loop to calculate the distance the vehicle has covered for each hour.
  4. Display the outcome in the format given, with hours and the corresponding distance traveled.


The example calculation is as follows: If a vehicle travels at 40 mph for 3 hours, then the distance for each hour is: Hour 1: 40 miles, Hour 2: 80 miles, Hour 3: 120 miles. This is repeated for the time period entered by the user.

User Marin Takanov
by
8.0k points