# Find the speed of the stream from the speed of the man given in both upstream and downstream

• Last Updated : 17 May, 2021

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:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

• 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++

 #includeusing namespace std; void rate(float down, float up){         // Stream rate    float rate = 0.5 * (down - up);    cout << rate <<  " Km/hr";} // Driver Codeint main(){       // Distance and time downstream    float N1 = 3;    float X1 = 15;     // Distance and time upstream    float N2 = 2;    float X2 = 5;     // Rate of downstream and upstream    float Rate_downstream = X1 / N1;    float Rate_upstream = X2 / N2;     rate(Rate_downstream, Rate_upstream);       return 0;} // This code is contributed by Surbhi Tyagi.

## Java

 /*package whatever //do not write package name here */ import java.io.*; public class GFG{     public static void rate(float down, float up){         // Stream rate    double rate = 0.5 * (down - up);    System.out.println(rate+ " Km/hr");}  // Driver Codepublic static void main(String args[]){   // Distance and time downstreamfloat N1 = 3;float X1 = 15; // Distance and time upstreamfloat N2 = 2;float X2 = 5; // Rate of downstream and upstreamfloat Rate_downstream = X1 / N1;float Rate_upstream = X2 / N2; rate(Rate_downstream, Rate_upstream);    }} // This code is contributed by sravankumar8128.

## Python3

 def rate(down, up):     # stream rate    rate = 0.5*(down - up)    print(rate, " Km/hr")  # Driver Code# Distance and time downstreamN1 = 3X1 = 15 # Distance and time upstreamN2 = 2X2 = 5 # Rate of downstream and upstreamRate_downstream = X1/N1Rate_upstream = X2/N2 rate(Rate_downstream, Rate_upstream)

## Javascript

 

Output:

1.25  Km/hr

My Personal Notes arrow_drop_up