Speed of boat upstream and downstream

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right



Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.