📌  相关文章
📜  以公里/小时为单位将速度转换为米/秒的程序,反之亦然

📅  最后修改于: 2022-05-13 01:57:58.640000             🧑  作者: Mango

以公里/小时为单位将速度转换为米/秒的程序,反之亦然

将以公里/小时为单位的速度转换为米/秒,反之亦然。
例子:

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 

方法:
1 公里 = 1000 米,1 小时 = 3600 秒(每个 60 分钟 x 60 秒)。
从 Km/h 转换为 m/sec-
将 km/hr 转换为 m/sec 时,将速度乘以 1000,然后将结果除以 3600。

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

从 m/sec 转换为 km/h-
将 m/sec 转换为 km/hr 时,将速度除以 1000,然后将结果乘以 3600。

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

下面是程序实现:

C++
// CPP program to convert 
// kmph to mps and vice versa
#include 
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


Javascript


输出:

speed_in_mps = 20 speed_in_kmph = 36