Find the speed of the stream from the speed of the man given in both upstream and downstream
A boat takes N1 hr to row a bot X1 km downstream of a river and take N2 hr to cover a distance of X2 km upstream. Find the speed of the stream.
Input: 3 15 2 5
Output: 17.5 km/hr
Input: 4 29 7 30
Output: 47 km/hr
Approach:
- Take input from users
- Calculate the rate of downstream and upstream. The rate can be calculated using the formula.
- Then, calculate the speed of the stream. It is given by the formula –
Below is the implementation.
C++
#include<iostream>
using namespace std;
void rate( float down, float up)
{
float rate = 0.5 * (down - up);
cout << rate << " Km/hr" ;
}
int main()
{
float N1 = 3;
float X1 = 15;
float N2 = 2;
float X2 = 5;
float Rate_downstream = X1 / N1;
float Rate_upstream = X2 / N2;
rate(Rate_downstream, Rate_upstream);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
public static void rate( float down, float up)
{
double rate = 0.5 * (down - up);
System.out.println(rate+ " Km/hr" );
}
public static void main(String args[])
{
float N1 = 3 ;
float X1 = 15 ;
float N2 = 2 ;
float X2 = 5 ;
float Rate_downstream = X1 / N1;
float Rate_upstream = X2 / N2;
rate(Rate_downstream, Rate_upstream);
}
}
|
Python3
def rate(down, up):
rate = 0.5 * (down - up)
print (rate, " Km/hr" )
N1 = 3
X1 = 15
N2 = 2
X2 = 5
Rate_downstream = X1 / N1
Rate_upstream = X2 / N2
rate(Rate_downstream, Rate_upstream)
|
Javascript
<script>
function rate(down, up)
{
var rate = 0.5 * (down - up);
document.write(rate, " Km/hr" );
}
var N1 = 3;
var X1 = 15;
var N2 = 2;
var X2 = 5;
var Rate_downstream = X1 / N1;
var Rate_upstream = X2 / N2;
rate(Rate_downstream, Rate_upstream);
</script>
|
C#
using System;
class GFG {
static double rate( float down, float up)
{
double rate = 0.5 * (down - up);
return rate;
}
public static void Main()
{
float N1 = 3;
float X1 = 15;
float N2 = 2;
float X2 = 5;
float Rate_downstream = X1 / N1;
float Rate_upstream = X2 / N2;
Console.WriteLine(
rate(Rate_downstream, Rate_upstream)
+ " Km/hr" );
}
}
|
Output:
1.25 Km/hr
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
19 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...