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 .

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; ` `} ` |

*chevron_right*

*filter_none*

## 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"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Length of bridge = 420 meters

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to find the Speed of train as per speed of sound
- Find the speed of the stream from the speed of the man given in both upstream and downstream
- Program to print reverse character bridge pattern
- Find minimum speed to finish all Jobs
- Calculate speed, distance and time
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Length of diagonals of a Rhombus using length of Side and vertex Angle
- Find the length of the median of a Triangle if length of sides are given
- Length of Diagonals of a Cyclic Quadrilateral using the length of Sides.
- Program to find if two numbers and their AM and HM are present in an array using STL
- Find Nth even length palindromic number formed using digits X and Y
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- C program to find the length of a string
- Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Length of longest subarray of length at least 2 with maximum GCD
- Minimize number of cuts required to break N length stick into N unit length sticks
- Program to find remainder without using modulo or % operator

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.