Speed of boat upstream and downstream - GeeksforGeeks

15 Apr, 2021
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++
Java
Python3
C#
Javascript
// CPP program to find upstream and// downstream speeds of a boat.#include <iostream>using namespace std; // Function to calculate the// speed of boat downstreamint Downstream(int b, int s){    return (b + s);} // Function to calculate the// speed of boat upstreamint Upstream(int b, int s){    return (b - s);} // Driver functionint 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 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 program to find upstream # and downstream speeds of a boat. # Function to calculate the#speed of boat downstreamdef Downstream(b, s):    return (b + s) # Function to calculate the# speed of boat upstreamdef Upstream(b, s):    return (b - s) # Driver Code # Speed of the boat in still water(B)# and speed of the stream (S) in km/hrB = 10; S = 4print("Speed Downstream = ", Downstream(B, S), " km/hr",      "\nSpeed Upstream = ", Upstream(B, S), " km/hr")       # This code is contributed by Anant Agarwal.
// 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.
<script> // Javascript program to find upstream and// downstream speeds of a boat.   // Function to calculate the// speed of boat downstreamfunction Downstream(b, s){    return (b + s);} // Function to calculate the// speed of boat upstreamfunction Upstream(b, s){    return (b - s);}   // Driver Code // Speed of the boat in still water(B)// and speed of the stream (S) in km/hrvar 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
 
bunnyram19
Time Speed Distance
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Exceptions in Java
Ternary Operator in Python
Destructors in C++
Difference between Abstract Class and Interface in Java
Inline Functions in C++
Exception Handling in C++
Pure Virtual Functions and Abstract Classes in C++
Python Exception Handling
'this' pointer in C++