Home »
Java Programs »
Java Most Popular & Searched Programs
Java program to convert distance from miles to kilometre and kilometre to miles
Java program to convert distance from miles to kilometre and kilometre to miles
This Java program can be used as distances converter, this program will convert distance from miles to kilometres and kilometres to miles.
package com.includehelp;
import java.util.Scanner;
/**
* Program to convert distance Miles to KM and KM to Miles
* @author includehelp
*/
public class DistanceConverter {
/**
* Method to get distance in miles and convert into Kilometers
* @param miles
* @return
*/
static double convertIntoKms(double miles){
double km=1.609*miles;
return km;
}
/**
* Method to get distance into km and convert into miles
* @param km
* @return
*/
static double convertIntoMiles(double km){
double miles=km/1.609;
return miles;
}
public static void main(String[] args) {
try{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Distance in Miles : ");
double miles = sc.nextDouble();
System.out.println(miles+" Miles equal to : "+convertIntoKms(miles)+" KMs");
System.out.print("Enter Distance in Km : ");
double kms = sc.nextDouble();
System.out.println(kms+" KMs equal to : "+convertIntoMiles(kms)+" Miles");
}
catch(Exception E){
System.err.println("Exception : "+E.getMessage());
}
}
}
Output
Enter Distance in Miles : 2
2.0 Miles equal to : 3.218 KMs
Enter Distance in Km : 5
5.0 KMs equal to : 3.107520198881293 Miles
Java Most Popular & Searched Programs »