Write a program to determine speed of the boat in still water(B) given the speed of the stream(S in km/hr), the time taken (for same point) by the boat upstream(T1 in hr) and downstream(T2 in hr).
Examples:
Input : T1 = 4, T2 = 2, S = 5
Output : B = 15
Input : T1 = 6, T2 = 1, S = 7
Output : B = 9.8
Prerequisite : Speed of boat upstream and downstream
Speed of boat in still water can be computed using below formula.
B = S*((T1 + T2) / (T1 – T2))
How does this formula work?
Since the point is same, distance traveled during upstream should be same as downstream.
Therefore,
(B – S) * T1 = (B + S) * T2
B(T1 – T2) = S*(T1 + T2)
B = S*(T1 + T2)/(T1 – T2)
C++
#include <iostream>
using namespace std;
float still_water_speed( float S, float T1, float T2)
{
return (S * (T1 + T2) / (T1 - T2));
}
int main()
{
float S = 7, T1 = 6, T2 = 1;
cout << "The speed of boat in still water = " <<
still_water_speed(S, T1, T2)<< " km/ hr " ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static float still_water_speed( float S, float T1, float T2)
{
return (S * (T1 + T2) / (T1 - T2));
}
public static void main (String[] args) {
float S = 7 , T1 = 6 , T2 = 1 ;
System.out.println( "The speed of boat in still water = " +
still_water_speed(S, T1, T2)+ " km/ hr " );
}
}
|
Python3
def still_water_speed(S, T1, T2):
return (S * (T1 + T2) / (T1 - T2))
S = 7 ; T1 = 6 ; T2 = 1
print ( "The speed of boat in still water = " ,
still_water_speed(S, T1, T2), " km/ hr " )
|
C#
using System;
class GFG {
static float still_water_speed( float S, float T1,
float T2)
{
return (S * (T1 + T2) / (T1 - T2));
}
public static void Main()
{
float S = 7, T1 = 6, T2 = 1;
Console.WriteLine( "The speed of boat in still water = " +
still_water_speed(S, T1, T2) + " km/ hr " );
}
}
|
PHP
<?PHP
function still_water_speed( $S , $T1 , $T2 )
{
return ( $S * ( $T1 + $T2 ) /
( $T1 - $T2 ));
}
$S = 7;
$T1 = 6;
$T2 = 1;
echo ( "The speed of boat in still water = " .
still_water_speed( $S , $T1 , $T2 ) .
" km/hr " );
?>
|
Javascript
<script>
function still_water_speed(S, T1, T2)
{
return (S * (T1 + T2) / (T1 - T2));
}
var S = 7;
var T1 = 6;
var T2 = 1;
document.write( "The speed of boat in still water = " +
still_water_speed(S, T1, T2) + " km/hr " );
</script>
|
Output:
The speed of boat in still water = 9.8 km/hr
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Feb, 2023
Like Article
Save Article