Open In App

Speed of boat upstream and downstream

Improve
Improve
Like Article
Like
Save
Share
Report

Write a program to find the speed of boat upstream and downstream. In order to calculate the speed of the boat upstream and downstream, we should know the speed of the boat in still water(B) and speed of the stream (S)

Examples: 

Input : B = 10, S = 4
Output : Speed Downstream = 14 km/hr 
         Speed Upstream = 6 km/hr 

Input : B = 12, S = 5
Output : Speed Downstream = 17 km/hr 
         Speed Upstream = 7 km/hr 

Approach : The direction along the stream is called downstream and the direction opposite the stream is called upstream . So in case of downstream the speeds will be added, while in case of upstream the speeds will be subtracted . 
The speed of boat downstream is equal to the sum of the speed of the boat in still water and speed of the stream . 
The speed of boat upstream is equal to the difference of the speed of the boat in still water and speed of the stream .Hence : 
Speed Downstream = B + S km/hr 
Speed Upstream = B – S km/hr 

C++




// CPP program to find upstream and 
// downstream speeds of a boat.
#include <iostream>
using namespace std;
  
// Function to calculate the
// speed of boat downstream
int Downstream(int b, int s)
{
    return (b + s);
}
  
// Function to calculate the
// speed of boat upstream
int Upstream(int b, int s)
{
    return (b - s);
}
  
// Driver function
int main()
{
    // Speed of the boat in still water(B)
    // and speed of the stream (S) in km/hr
    int B = 10, S = 4;
   cout << "Speed Downstream = " << Downstream(B, S)
        << " km/hr\n"<< "Speed Upstream = " << 
            Upstream(B, S) << " km/hr";
    return 0;
}


Java




// Java program to find upstream and 
// downstream speeds of a boat.
import java.io.*;
  
class GFG 
{
    // Function to calculate the
    // speed of boat downstream
    static int Downstream(int b, int s)
    {
        return (b + s);
    }
      
    // Function to calculate the
    // speed of boat upstream
    static int Upstream(int b, int s)
    {
        return (b - s);
    }
      
    // Driver function
    public static void main (String[] args) {
        // Speed of the boat in still water(B)
        // and speed of the stream (S) in km/hr
        int B = 10, S = 4;
        System.out.println ("Speed Downstream = " + Downstream(B, S)
                             + " km/hr\n"+ "Speed Upstream = "
                            Upstream(B, S) + " km/hr");
      
    }
}
  
// This code is contributed by vt_m.


Python3




# Python3 program to find upstream  
# and downstream speeds of a boat.
  
# Function to calculate the
#speed of boat downstream
def Downstream(b, s):
    return (b + s)
  
# Function to calculate the
# speed of boat upstream
def Upstream(b, s):
    return (b - s)
  
# Driver Code
  
# Speed of the boat in still water(B)
# and speed of the stream (S) in km/hr
B = 10; S = 4
print("Speed Downstream = ", Downstream(B, S), " km/hr",
      "\nSpeed Upstream = ", Upstream(B, S), " km/hr")
        
# This code is contributed by Anant Agarwal.


C#




// C# program to find upstream and
// downstream speeds of a boat.
using System;
  
class GFG {
      
    // Function to calculate the
    // speed of boat downstream
    static int Downstream(int b, int s)
    {
        return (b + s);
    }
  
    // Function to calculate the
    // speed of boat upstream
    static int Upstream(int b, int s)
    {
        return (b - s);
    }
  
    // Driver function
    public static void Main()
    {
        // Speed of the boat in still water(B)
        // and speed of the stream (S) in km/hr
        int B = 10, S = 4;
        Console.WriteLine("Speed Downstream = " + Downstream(B, S) +
                          " km/hr\n" + "Speed Upstream = "
                          Upstream(B, S) + " km/hr");
    }
}
  
// This code is contributed by vt_m.


Javascript




<script>
  
// Javascript program to find upstream and
// downstream speeds of a boat.
    
// Function to calculate the
// speed of boat downstream
function Downstream(b, s)
{
    return (b + s);
}
  
// Function to calculate the
// speed of boat upstream
function Upstream(b, s)
{
    return (b - s);
}
    
// Driver Code
  
// Speed of the boat in still water(B)
// and speed of the stream (S) in km/hr
var B = 10;
var S = 4;
  
document.write("Speed Downstream = "
               Downstream(B, S) +" km/hr"
               "<br>" + "Speed Upstream = " +  
               Upstream(B, S) + " km/hr");
                 
// This code is contributed by bunnyram19
  
</script>


Output: 

Speed Downstream = 14 km/hr 
Speed Upstream = 6 km/hr

Time Complexity: O(1)

Auxiliary Space: O(1)
 



Last Updated : 17 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads