Convert speed given in km/hr to m/sec and vice versa.
Examples:
Input : kmph = 72, mps = 10 Output : speed_in_mps = 20 speed_in_kmph = 36 Input : kmph = 54, mps = 15 Output : speed_in_mps = 15 speed_in_kmph = 54
Approach:
1 km = 1000 m and 1 hr = 3600 sec (60 min x 60 sec each).
Conversion from Km/h to m/sec-
When converting km/hr to m/sec, multiply the speed with 1000 and divide the result with 3600.
1 km/hr = 5/18 m/sec or 0.277778 m/sec
Conversion from m/sec to km/h-
When Converting m/sec to km/hr, divide the speed by 1000 and multiply the result by 3600.
1 m/sec = 18/5 km/hr or 3.6 km/hr
Below is the program implementation:
C++
// CPP program to convert // kmph to mps and vice versa #include <bits/stdc++.h> using namespace std;
// function to convert speed // in km/hr to m/sec float kmph_to_mps( float kmph)
{ return (0.277778 * kmph);
} // function to convert speed // in m/sec to km/hr float mps_to_kmph( float mps)
{ return (3.6 * mps);
} // driver function int main()
{ // variable to store
// speed in kmph
float kmph = 72.0;
// variable to store
// speed in mps
float mps = 10.0;
cout << "speed_in_mps = " << kmph_to_mps(kmph)
<< " speed_in_kmph = " << mps_to_kmph(mps);
return 0;
} |
Java
// Java program to convert // kmph to mps and vice versa import java.io.*;
class GFG
{ // function to convert speed
// in km/hr to m/sec
static int kmph_to_mps( double kmph)
{
return ( int ) ( 0.277778 * kmph);
}
// function to convert speed
// in m/sec to km/hr
static int mps_to_kmph( double mps)
{
return ( int ) ( 3.6 * mps);
}
// Driver function
public static void main (String[] args)
{
// variable to store
// speed in kmph
double kmph = 72.0 ;
// variable to store
// speed in mps
double mps = 10.0 ;
System.out.println( "speed_in_mps = " + kmph_to_mps(kmph)
+ " speed_in_kmph = " +mps_to_kmph(mps));
}
} // This code is contributed by vt_m. |
Python3
# Python3 program to convert # kmph to mps and vice versa # Function to convert speed # in km/hr to m/sec def kmph_to_mps(kmph):
return ( 0.277778 * kmph)
# Function to convert speed # in m/sec to km/hr def mps_to_kmph(mps):
return ( 3.6 * mps)
# Driver Code # variable to store # speed in kmph kmph = 72.0
# variable to store # speed in mps mps = 10.0
print ( "speed_in_mps = " , int (kmph_to_mps(kmph)) ,
" speed_in_kmph = " , int (mps_to_kmph(mps)))
# This code is contributed by Anant Agarwal. |
C#
// C# program to convert // kmph to mps and vice versa using System;
class GFG {
// function to convert speed
// in km/hr to m/sec
static int kmph_to_mps( double kmph)
{
return ( int )(0.277778 * kmph);
}
// function to convert speed
// in m/sec to km/hr
static int mps_to_kmph( double mps)
{
return ( int )(3.6 * mps);
}
// Driver function
public static void Main()
{
// variable to store
// speed in kmph
double kmph = 72.0;
// variable to store
// speed in mps
double mps = 10.0;
Console.WriteLine( "speed_in_mps = " + kmph_to_mps(kmph) +
" speed_in_kmph = " + mps_to_kmph(mps));
}
} // This code is contributed by vt_m. |
PHP
<?php // PHP program to convert // kmph to mps and vice versa // function to convert speed
// in km/hr to m/sec
function kmph_to_mps( $kmph )
{
return floor (0.277778 * $kmph );
}
// function to convert speed
// in m/sec to km/hr
function mps_to_kmph( $mps )
{
return (3.6 * $mps );
}
// Driver Code
// variable to store
// speed in kmph
$kmph = 72.0;
// variable to store
// speed in mps
$mps = 10.0;
echo "speed_in_mps = " ,
kmph_to_mps( $kmph )
, " speed_in_kmph = " ,
mps_to_kmph( $mps );
// This code is contributed by anuj_67. ?> |
Javascript
<script> // javascript program to convert // kmph to mps and vice versa // Function to convert speed // in km/hr to m/sec function kmph_to_mps(kmph)
{ return (0.277778 * kmph);
} // Function to convert speed // in m/sec to km/hr function mps_to_kmph(mps)
{ return (3.6 * mps) ;
} // Driver Code // variable to store // speed in kmph var kmph = 72.0
// variable to store // speed in mps var mps = 10.0
document.write( "speed_in_mps = " + Number(kmph_to_mps(kmph).toFixed(0)) +
" speed_in_kmph = " + Number(mps_to_kmph(mps).toFixed(0)))
// This code is contributed by bunnyram19.
</script> |
Output
speed_in_mps = 20 speed_in_kmph = 36
Time Complexity: O(1)
Auxiliary Space: O(1)