Skip to content
Related Articles

Related Articles

Program to find Length of Bridge using Speed and Length of Train
  • Difficulty Level : Easy
  • Last Updated : 13 Apr, 2021

Given the Length and Speed of the train and the time takes by the train to passes the bridge or tunnel, the task is to find the length of the bridge.
Examples: 
 

Input : length of train = 120 meters, Speed = 30 m/sec, time = 18 sec 
Output : length of bridge = 420 meters.
Input : length of train = 130 meters, Speed = 25 m/sec, time = 21 sec 
Output : length of bridge = 395 meters. 
 

 

Approach: 
Let the length of the bridge be x
As it is known that Speed = Distance / Time 
Therefore: 
 

Total distance = (length of train + length of bridge) 
=> Speed of train = (length of train + x) / Time. 
=> x = (Speed of train * time) – Length of train.



Formula: 
 

length of bridge = (speed of train * time taken to cross bridge) – length of train.

Below is the implementation of the above approach: 
 

C++




// C++ Program to implement above code.
 
#include <bits/stdc++.h>
using namespace std;
 
// function to calculate the length of bridge.
int bridge_length(int trainLength, int Speed, int Time)
{
    return ((Time * Speed) - trainLength);
}
 
// Driver Code
int main()
{
    // Assuming the input variables
    int trainLength = 120;
    int Speed = 30;
    int Time = 18;
 
    cout << "Length of bridge = "
         << bridge_length(trainLength, Speed, Time)
         << " meters";
 
    return 0;
}

Java




//Java Program to implement above code.
 
public class GFG {
     
    //function to calculate the length of bridge.
    static int bridge_length(int trainLength,
                            int Speed, int Time)
    {
    return ((Time * Speed) - trainLength);
    }
 
    //Driver Code
    public static void main(String[] args) {
         
        // Assuming the input variables
        int trainLength = 120;
        int Speed = 30;
        int Time = 18;
 
        System.out.print("Length of bridge = "+
            bridge_length(trainLength, Speed,Time)
            +" meters");
    }
}

Python 3




# Python 3 Program to implement above code.
 
# function to calculate the length of bridge.
def bridge_length(trainLength, Speed, Time) :
 
    return ((Time * Speed) - trainLength)
     
# Driver Code
if __name__ == "__main__" :
 
    # Assuming the input variables
    trainLength = 120
    Speed = 30
    Time = 18
 
    print("Length of bridge = ",bridge_length
            (trainLength, Speed, Time),"meters")
 
# This code is contributed by ANKITRAI1

C#




// C# Program to implement above code
using System;
 
class GFG
{
 
// function to calculate
// the length of bridge
static int bridge_length(int trainLength,
                         int Speed, int Time)
{
    return ((Time * Speed) - trainLength);
}
 
// Driver Code
static void Main()
{
    // Assuming the input variables
    int trainLength = 120;
    int Speed = 30;
    int Time = 18;
 
        Console.Write("Length of bridge = " +
        bridge_length(trainLength, Speed, Time) +
                                      " meters");
}
}
 
// This code is contributed by Raj

PHP




<?php
// PHP Program to implement
// above code
 
// function to calculate
// the length of bridge
function bridge_length($trainLength,
                       $Speed, $Time)
{
    return (($Time * $Speed) -
             $trainLength);
}
 
// Driver Code
 
// Assuming the input variables
$trainLength = 120;
$Speed = 30;
$Time = 18;
 
echo "Length of bridge = ".
    bridge_length($trainLength,
                  $Speed, $Time).
                       " meters";
 
// This code is contributed by Raj
?>

Javascript




<script>
 
// Javascript program to implement above code.
 
// Function to calculate the length of bridge.
function bridge_length(trainLength, Speed, Time)
{
    return ((Time * Speed) - trainLength);
}
 
// Driver code
 
// Assuming the input variables
var trainLength = 120;
var Speed = 30;
var Time = 18;
 
document.write("Length of bridge = " +
   bridge_length(trainLength, Speed,Time) +
   " meters");
 
// This code is contributed by Khushboogoyal499
    
</script>
Output: 
Length of bridge = 420 meters

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :