Write a program to determine the distance(D) between two points traveled by a boat, given the speed of boat in still water(B), the speed of the stream(S), the time taken to row a place and come back i.e T.

**Examples:**

Input : B = 5, S = 1, T = 1 Output : D = 2.4 Input : B = 5, S = 2, T = 1 Output : D = 2.1

Formula for distance is D = T*(B^2 – S^2)/ (2*B)

**How does this formula work?**

Time taken when person goes upstream, t1 = D/(B-S) Time taken when person goes downstream, t2 = D/(B+S) We are given t1 + t2 = T So, D/(B-S) + D/(B+S) = T D = T*(B^2 - S^2)/ (2*B)

## C++

`// CPP program to find distance between ` `// two points using boat speed, stream ` `// speed and total time. ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to calculate the distance ` `// between two points via boat ` `float` `distance(` `float` `T, ` `float` `B, ` `float` `S) ` `{ ` ` ` `return` `(T * (((B * B) - (S * S)) / (2 * B))); ` `} ` ` ` `int` `main() ` `{ ` ` ` `// Time taken time taken to row a place ` ` ` `// and come back in hr ` ` ` `float` `T = 1; ` ` ` ` ` `// Speed of boat in still water in km/hr ` ` ` `float` `B = 5; ` ` ` ` ` `// Speed of stream in km/hr ` ` ` `float` `S = 1; ` ` ` `cout << ` `"The distance between two points via boat = "` ` ` `<< distance(T, B, S) << ` `" km"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to find distance between ` `// two points using boat speed, stream ` `// speed and total time. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate the distance ` ` ` `// between two points via boat ` ` ` `static` `float` `distance(` `float` `T, ` `float` `B, ` `float` `S) ` ` ` `{ ` ` ` `return` `(T * (((B * B) - (S * S)) / (` `2` `* B))); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `// Time taken time taken to row a place ` ` ` `// and come back in hr ` ` ` `float` `T = ` `1` `; ` ` ` ` ` `// Speed of boat in still water in km/hr ` ` ` `float` `B = ` `5` `; ` ` ` ` ` `// Speed of stream in km/hr ` ` ` `float` `S = ` `1` `; ` ` ` `System.out.println(` `"The distance between two points via boat = "` ` ` `+ distance(T, B, S) + ` `" km"` `); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find distance ` `# between two points using boat speed, ` `# stream speed and total time. ` ` ` `# Function to calculate the distance ` `# between two points via boat ` `def` `distance( T, B, S): ` ` ` ` ` `return` `(T ` `*` `(((B ` `*` `B) ` `-` `(S ` `*` `S)) ` `/` `(` `2` `*` `B))) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Time taken time taken to row ` ` ` `# a place and come back in hr ` ` ` `T ` `=` `1` ` ` ` ` `# Speed of boat in still ` ` ` `# water in km/hr ` ` ` `B ` `=` `5` ` ` ` ` `# Speed of stream in km/hr ` ` ` `S ` `=` `1` ` ` `print` `(` `"The distance between two "` `+` ` ` `"points via boat ="` `, ` ` ` `distance(T, B, S), ` `"km"` `) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find distance between ` `// two points using boat speed, stream ` `// speed and total time. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to calculate the distance ` ` ` `// between two points via boat ` ` ` `static` `float` `distance(` `float` `T, ` `float` `B, ` `float` `S) ` ` ` `{ ` ` ` `return` `(T * (((B * B) - (S * S)) / (2 * B))); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// Time taken time taken to row a place ` ` ` `// and come back in hr ` ` ` `float` `T = 1; ` ` ` ` ` `// Speed of boat in still water in km/hr ` ` ` `float` `B = 5; ` ` ` ` ` `// Speed of stream in km/hr ` ` ` `float` `S = 1; ` ` ` `Console.WriteLine(` `"The distance between two points via boat = "` `+ ` ` ` `distance(T, B, S) + ` `" km"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

**Output:**

The distance between two points via boat = 2.4 km

## Recommended Posts:

- Speed of boat in still water from speed of stream and times taken
- Speed of boat upstream and downstream
- Program to calculate distance between two points
- Program to calculate distance between two points in 3 D
- Minimum number of points to be removed to get remaining points on one side of axis
- Time until distance gets equal to X between two objects moving in opposite direction
- Check if two people starting from different points ever meet
- Distance Traveled by Two Trains together in the same Direction
- Find maximum and minimum distance between magnets
- Perpendicular distance between a point and a Line in 2 D
- Distance between a point and a Plane in 3 D
- Coordinates of rectangle with given points lie inside
- Program to check if the points are parallel to X axis or Y axis
- Program to find equation of a plane passing through 3 points
- Maximum points of intersection n lines
- Convert a given temperature to another system based on given boiling and freezing points
- QA - Placement Quizzes | Time Speed Distance | Question 1
- QA - Placement Quizzes | Time Speed Distance | Question 2
- QA - Placement Quizzes | Time Speed Distance | Question 3
- QA - Placement Quizzes | Time Speed Distance | Question 4

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.