A passenger train travels at a speed of 72 km/h. A man on the passenger train observes a
goods train travelling at a speed of 54 km/h in the opposite direction. If the good train passes him in X
seconds, write a C++ function to find the length of the goods train.
#include <iostream>
using namespace std;
int main(){
  int rspeed;
  rspeed = 72 + 54;
  float time;
  cout<<"Input the time in seconds taken to pass the good train: ";
  cin>>time;
  float length = (float) rspeed*1000/3600 * time; //convert speed to m/s because time is in seconds
  cout<<"Length of the good train = "<<length<<" meters.\n";
  return 0;
}
Comments
Leave a comment