How to convert kilometers to miles using Java program?
Contents
Distance Units
In school days, we might used ruler to measure the distance between two points. The ruler has the units such as millimeter and centimeter through which we measured the distance. Similarly we have other units such as kilometers and miles to measure the distance between two places.
Example : Distance from New York to California in USA
Convert Kilometers to Miles
Most of the countries including Japan, China, India, Australia and UAE are using kilometers to measure the distance. On the other hand, countries like USA and UK are using miles to measure the distance. Lets understand how to convert kilometers to miles using standard values.
1 mile is equals to 1.609344 kilometers => 1 mile ~ 1.6 kilometers (approximately)
If we want to convert from kilometers to miles, we need to divide the kilometers by 1.609344.
1 kilometer is equals to 1/1.609344 => 0. 621371 miles
Java program to convert kilometers to miles
In this example, we will show you how to write a java program to convert kilometers to miles. The program gets the kilometer value from the user and store it in the variable. Then it is dividing the kilometer value by 1.609344 which will give the values in terms of miles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; public class UnitConversion { public static void main(String[] args) { // Declaring the variables double kiloMeters,miles; // 1 mile = 1.609344 kilometers => 1 kilometer = 1/1.609344 miles. double conversionFactor = 1.609344; // Getting user input using Scanner class System.out.println("Enter distance value in Kilometers : "); Scanner input = new Scanner(System.in); kiloMeters = input.nextDouble(); // To convert kilometers to miles, dividing the kilometers by 1.609344 miles = kiloMeters / conversionFactor; //printing the output System.out.println("The distance in Miles : " + miles); } } |
Output
1 2 3 |
Enter distance value in Kilometers : 355 The distance in Miles : 220.58677324425355 |
Recommended Articles