The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For example, if a train travels 40 miles-per-hour for three hours, the distance traveled is 120 miles. Write a program that asks for the speed of a vehicle (in miles-per-hour) and the number of hours it has traveled. Both values are assumed to be integers. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. For example, if a vehicle is traveling at 40 mph for a three-hour time period, it should display a report similar to the one that follows: Hours Distance Traveled --------------------------- 1 40 2 80 3 120 Do not accept a negative number for speed and do not accept any value less than 1 for time traveled.

Respuesta :

Answer:

// here is code in C++.

// include headers

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{   // variable

   double speed,hour;

   cout<<"enter the speed of vehicle:";

   // read speed

   cin>>speed;

   // check for the negative speed

   while(speed<0)

   {   // if speed is negative then again take input

       cout<<"Speed can't be negative!!"<<endl;

       cout<<"Enter the Speed again:";

       cin>>speed;

   }

   //ask for hours

   cout<<"enter hours traveled:";

   //read hours

   cin>>hour;

   // check hours<1 or not

   while(hour<1)

   {

       cout<<"hour can't be less than 1!!"<<endl;

       cout<<"Enter the Hours again:";

       cin>>hour;

   }

   // print the distance after each hour

   cout<<"Hours \t Distance Traveled "<<endl;

   cout<<"---------------------------"<<endl;

   for(int h=1;h<=hour;h++)

   {

       cout<<h<<"\t"<<speed*h<<endl;

   }

return 0;

}

Explanation:

Read the speed of the vehicle and assign it to variable "speed".Then read the hours traveled and assign to "hour".After this print the each hour and distance traveled by multiplying speed with h .

Output:

enter the speed of vehicle:-10

Speed can't be negative!!

Enter the Speed again:40

enter hours traveled:.4

hour can't be less than 1!!

Enter the Hours again:3

Hours    Distance Traveled

---------------------------

1       40

2       80

3       120

ACCESS MORE