Home »
.Net »
C# Programs
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#?
By Nidhi Last updated : April 15, 2023
Meter to Kilometre Conversion (Vice-Versa)
Here we will read the meter value which is converted into kilometer and then read the value kilometer and then converted into meters.
C# code for meter to kilometre conversion (vice-versa)
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.
C# Basic Programs »