Q:

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

belongs to collection: C# Basic Programs | basics

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to convert a decimal number into a hexa... >>
<< C# program to convert a temperature from Celsius t...