C# program to convert a meter into kilo-meter and vice versa

Here, we are going to learn how to convert a meter into kilo-meter and vice versa in C#?
Submitted by Nidhi, on September 14, 2020

Here we will read the meter value which is converted into kilometer and then read the value kilometer and then converted into meters.

Program:

The source code to convert a meter into kilo-meter and vice versa is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to convert a meter to kilo-meter and vice versa.

using System;

public class Distance
{
    public static double MeterToKilometer(double meter)
    {
        double KM = 0;

        KM = meter / 1000;

        return KM;
    }

    public static double KilometerToMeter(double km)
    {
        double METER = 0;

        METER = km * 1000;
 
        return METER;
    }

    static void Main()
    {
        double meter = 0;
        double km    = 0;

        Console.Write("Enter the value of meter : ");
        meter = double.Parse(Console.ReadLine());

        km = MeterToKilometer(meter);
        Console.WriteLine("Kilometer : "+km+"km");

        Console.Write("Enter the value of kilometer : ");
        km = double.Parse(Console.ReadLine());

        meter = KilometerToMeter(km);
        Console.WriteLine("Meter : " + meter + "m");

    }
}

Output:

Enter the value of meter : 2300
Kilometer : 2.3km
Enter the value of kilometer : 2.34
Meter : 2340m
Press any key to continue . . .

Explanation:

In the above program, we created a class Distance that contains three static methods MeterToKilometer(), KilometerToMeter(), Main() method. The MeterToKilometer() method is used to convert meters into kilometers and the KilometerToMeter() method is used to convert kilometers into meters.

In the Main() method, we declared two local variables meter and km then we read the meter value which is converted into kilometer and then read the value kilometer and then converted into meters.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.