C# Program to Convert Meter to Kilometer and Viceversa
Posted by Superadmin on August 11 2022 08:24:52

C# Program to Convert Meter to Kilometer and Viceversa

 

 

This is a C# Program to perform conversions of meter to kilometer and viceversa.

Problem Description

This C# Program Performs Conversions of Meter to Kilometer and vice versa.

Problem Solution

Here the entered meter value is converted into kilometer and the entered kilometer is converted in terms of meter.

Program/Source Code

Here is source code of the C# Program to Perform Conversions of Meter to Kilometer and vice versa. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform Conversions of Meter to Kilometer and viceversa
 */
using System;
class Program
{
    static void Main()
    {
        double m1 = 200;
        Console.WriteLine("Meter   ::    Kilometer");
        double k1 = ConvertDistance.cMtK(m1);
        Console.WriteLine("{0}     ::    {1}", m1, k1);
        double m4 = 3107;
        double k4 = ConvertDistance.cMtK(m4);
        Console.WriteLine("{0}   ::     {1}", m4, k4);
        double k3 = 5.1;
        Console.WriteLine();
        Console.WriteLine("Kilometer     ::     Meter");
        double m3 = ConvertDistance.cKtM(k3);
        Console.WriteLine("{0}           ::     {1}", k3, m3);
        double k2 = 3.219;
        double m2 = ConvertDistance.cKtM(k2);
        Console.WriteLine("{0}         ::    {1}", k2, m2);
        Console.Read();
    }
}
 
public static class ConvertDistance
{
    public static double cMtK(double meters)
    {
        return meters / 1000;
    }
 
    public static double cKtM(double kilometers)
    {
        return kilometers * 1000;
    }
}
Program Explanation

In this C# program, the entered meter value is converted into kilometer, and the entered kilometer is converted in terms of meter by calling cMtk() function and cKtm() function by passing the miles and kilometers variable values as an argument. Print the conversions of meter to kilometer and viceversa.

 
Runtime Test Cases
 
Meter  ::  Kilometer
200    ::  0.200000
3107  ::  3.107
 
Kilometer ::  Meter
5.1       ::  5100
3.219     ::  3219