Speed of boat upstream and downstream
Last Updated :
17 Feb, 2023
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++
#include <iostream>
using namespace std;
int Downstream( int b, int s)
{
return (b + s);
}
int Upstream( int b, int s)
{
return (b - s);
}
int main()
{
int B = 10, S = 4;
cout << "Speed Downstream = " << Downstream(B, S)
<< " km/hr\n" << "Speed Upstream = " <<
Upstream(B, S) << " km/hr" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int Downstream( int b, int s)
{
return (b + s);
}
static int Upstream( int b, int s)
{
return (b - s);
}
public static void main (String[] args) {
int B = 10 , S = 4 ;
System.out.println ( "Speed Downstream = " + Downstream(B, S)
+ " km/hr\n" + "Speed Upstream = " +
Upstream(B, S) + " km/hr" );
}
}
|
Python3
def Downstream(b, s):
return (b + s)
def Upstream(b, s):
return (b - s)
B = 10 ; S = 4
print ( "Speed Downstream = " , Downstream(B, S), " km/hr" ,
"\nSpeed Upstream = " , Upstream(B, S), " km/hr" )
|
C#
using System;
class GFG {
static int Downstream( int b, int s)
{
return (b + s);
}
static int Upstream( int b, int s)
{
return (b - s);
}
public static void Main()
{
int B = 10, S = 4;
Console.WriteLine( "Speed Downstream = " + Downstream(B, S) +
" km/hr\n" + "Speed Upstream = " +
Upstream(B, S) + " km/hr" );
}
}
|
Javascript
<script>
function Downstream(b, s)
{
return (b + s);
}
function Upstream(b, s)
{
return (b - s);
}
var B = 10;
var S = 4;
document.write( "Speed Downstream = " +
Downstream(B, S) + " km/hr" +
"<br>" + "Speed Upstream = " +
Upstream(B, S) + " km/hr" );
</script>
|
Output:
Speed Downstream = 14 km/hr
Speed Upstream = 6 km/hr
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...