In: Computer Science
(c++) the constant velocity of an object moving in straight line is defined as r= d/t, where r is the speed, d is the distance and t is the time it takes to complete movement. write a program that will ask user to enter distance moved and the time needed to complete it. program should calculate speed of that object and print results on screen
Here is the C++ program, i have written condition to ensure we are not dividing the distance by zero. Here is screenshot and code
=================================================================================
#include<iostream>
using namespace std;
int main(void) {
double distance=0,time=0;
cout<<"Enter distance in (meters): ";
cin>>distance;
cout<<"Enter time in (seconds): ";
cin>>time;
if(time>0) {
double velocity = distance/time;
cout<<"Velocity: "<<velocity<<"
m/sec"<<endl;
} else {
cout<<"Velocity undefined"<<endl;
}
}
=================================================================================
thanks !