Miles to Kilometers Java Program

Miles to Kilometers Java Program | In this post we will discuss how to develop the miles to kilometers Java program. To convert miles to kilometers, as we know that 1 Mile = 1.60934 Kilometers.

Therefore the formula to convert miles to kilometers is (miles * 1.60934). By using this formula we can convert the given miles to kilometers. Both kilometers & miles are units of length. The kilometer is denoted by “km” whereas the miles are denoted by “m”. Kilo is usually prefixed by 1000. A mile is a unit of linear measurement which is equal to 1760 yards, which is approximately 1.609 kilometers.

The formula to convert miles to kilometers is as follows:-
Kilometers = Miles * 1.60934

Examples of a mile to kilometer conversion:-
1) Mile = 4
Kilometer = 4 * 1.60934 = 6.43738 KM

2) Mile = 10
Kilometer = 10 * 1.60934 = 16.0934 KM

Miles to Kilometers Java Program

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Please enter miles: ");
      double mile = scan.nextDouble();
      double kilometer = mile * 1.6;
      System.out.println("Kilometers: " + kilometer);
      scan.close();

   }
}

Output:-

Please enter miles: 4
Kilometers: 6.4

Please enter miles: 10
Kilometers: 16.0

Java Program to Convert Miles To Kilometers

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);

      System.out.print("Enter the Miles: ");
      double miles = scan.nextDouble();
      double kilometers = miles * 1.6093434;

      System.out.println("Distance in Miles: " 
           + miles + " Miles");
      System.out.println("Distance in KiloMeters: " 
           + kilometers + " KiloMeters");
      scan.close();
   }
}

Output:-

Enter the Miles: 5.5
Distance in Miles: 5.5 Miles
Distance in KiloMeters: 8.8513887 KiloMeters

Enter the Miles: 40
Distance in Miles: 40.0 Miles
Distance in KiloMeters: 64.373736 KiloMeters

Enter the Miles: 100
Distance in Miles: 100.0 Miles
Distance in KiloMeters: 160.93434 KiloMeters

In the above miles to kilometers Java program code we have used scanner class to take the input from the user, and have declared miles and kilometers as double, double takes 8 bytes hence it is used here. Then to convert the miles into kilometers we multiply the given miles by 1.6093434 and then displayed the resultant value. Also see:- Unicode character set in Java

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “Miles to Kilometers Java Program”

Leave a Comment

Your email address will not be published. Required fields are marked *