Open In App

Find speed of man from speed of stream and ratio of time with up and down streams

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Program to find the speed of man in km/hr, given the speed of stream in km/hr. Assume that the man takes n times (where n > 1) as long to row upstream as to row downstream the river.
Examples: 
 

Input : 5.5 4
Output : speed of man is 9.16667 km/hr

Input : 4 3
Output : speed of man is 8 km/hr

 

Approach Used :
Let speed of the man in still water = x km/hr 
Let speed of the stream = y km/hr 
Then, speed downstream = (x + y) km/hr 
speed upstream = (x – y) km/hr 
Let time taken to travel downstream = t hr 
Then, time taken to travel upstream = nt hr 
Distance travelled downstream = (x + y)t km ————-(1) 
Distance travelled upstream = (x – y)tn km ————-(2)
from (1) and (2) 
(x + y)t = (x – y)tn 
which gives : (x + y) = (x – y)n 
which gives : x + y = nx – ny 
which gives : nx – x = ny + y 
which gives : x(n – 1) = y(n + 1) 
which gives : x = y(n + 1)/(n – 1)
 

C++




// C++ implementation
#include <bits/stdc++.h>
using namespace std;
  
// function to return speed of man
float speed_man(float y1, int n1)
{
    // return the speed of man in km/hr
    return ( y1 * (n1 + 1) / (n1 - 1) );
  
}
  
// Driver function
int main()
{
    // y is speed of stream in km/hr
    float y = 2.3;
    int n = 6;
      
    float speed = speed_man(y, n);
    cout << "speed of man is " << speed
        << " km/hr" << endl;
  
    return 0;
}


Java




// Java Implementation
import java.lang.*;
import java.util.*;
  
public class GeeksforGeeks {
  
    // function to return speed of man
    static float speed_man(float y1, int n1)
    {
        // x is speed of man
        return ( y1 * (n1 + 1) / (n1 - 1) );
           
    }
  
    // Driver function
    public static void main(String[] args)
    {
  
        // y is speed of stream in km/hr
        float y = 2.3f;
        int n = 6;
        float speed = speed_man(y, n);
          
        System.out.println("speed of man is " 
                            + speed + " km/hr");
    }
}


Python3




   
# Python3 implementation to find
# the speed of man in km/hr
  
# Function to return speed of man
def speed_man(y1, n1):
  
    # return the speed of man in km/hr
    return (y1 * (n1 + 1) / (n1 - 1))
  
# Driver Code
  
# y is speed of stream in km/hr
y = 2.3; n = 6
      
speed = speed_man(y, n)
print("speed of man is "
      "%.2f"%speed, " km/hr")
  
# This code is contributed by Anant Agarwal.


C#




// C# Implementation
using System;
  
public class GFG {
  
    // function to return speed of man
    static float speed_man(float y1, int n1)
    {
          
        // x is speed of man
        return ( y1 * (n1 + 1) / (n1 - 1) );
    }
  
    // Driver function
    public static void Main()
    {
  
        // y is speed of stream in km/hr
        float y = 2.3f;
        int n = 6;
        float speed = speed_man(y, n);
          
        Console.WriteLine("speed of man is "
                        + speed + " km/hr");
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP implementation to find
// the speed of man in km/hr
  
// function to return 
// speed of man
function speed_man($y1, $n1)
{
      
    // return the speed of 
    // man in km/hr
    return ($y1 * ($n1 + 1) / 
                 ($n1 - 1) );
  
}
  
    // Driver function
    // y is speed of 
    // stream in km/hr
    $y = 2.3;
    $n = 6;
      
    $speed = speed_man($y, $n);
    echo "speed of man is " , $speed
    , " km/hr" ;
  
// This code is contributed by anuj_67.
?>


Javascript




<script>
    // javascript Implementation
  
    // function to return speed of man
    function speed_man( y1,  n1)
    {
            
        // x is speed of man
          
        return ( y1 * (n1 + 1) / (n1 - 1) );
    }
      
    
    // Driver function
  
    
        // y is speed of stream in km/hr
        var y = 2.3;
        var n = 6;
        var speed = speed_man(y, n).toFixed(2)
            
        document.write("speed of man is "  + speed + " km/hr");
  
</script>


Output: 
 

speed of man is 3.22 km/hr

Time Complexity: O(1)

Auxiliary Space: O(1)
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads