Program to convert speed in km/hr to m/sec and vice versa

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 with 1000 and multiply the result with 3600.

1 m/sec = 18/5 km/hr or 3.6 km/hr

Below is the program implementation:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output:

speed_in_mps = 20 speed_in_kmph = 36


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.



Improved By : vt_m



Article Tags :

Be the First to upvote.


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