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.

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.