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