Java Program to Convert Miles to Kilometers

In this post, we will learn to code the Java Program to Convert Miles to Kilometers. Let’s understand more about Miles to Kilometers conversion and How to Convert Miles to Kilometers in Java Programming Language.

What is Miles?

A mile is also a unit of length but it is popular in the imperial and US customary systems of measurement. It is currently defined as 5,280 feet, 1,760 yards, or exactly 1609.344 meters i.e. 1.609344 Km. The symbol of a mile is mi.

What is KIlometers?

A Kilometer is a unit of length in the metric system, which is equal to one thousand meters. It is now the measurement unit used for expressing distances between geographical places on land in most of the world. The SI symbol of a Kilometer is Km.

Miles(mi) to Kilometers(km) Conversion

1 Mile is exactly equal to 1609.344 meters which are when converted in kilometers is equal to 1.609344 Km.
Therefore,
1 Mile = 1.609344 Kilometers

 Mile = 1.609344 Kilometers

Now, let’s see the Java Program to Convert Miles to Kilometers.

Java Program to Convert Miles to Kilometers

import java.util.*;

public class Main{
    
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the Distance in Miles: ");
        float miles = sc.nextFloat();
        
        //Miles to Kilometers conversion
        float km = (float)(1.609344 * miles);
        
        System.out.println("Distance in kilometers will be: "+km);
        
    }
    
}

Output 1

Enter the Distance in Miles: 1
Distance in kilometers will be: 1.609344

Output 2

Enter the Distance in Miles: 5
Distance in kilometers will be: 8.04672

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Distance in Miles: ");
        float miles = sc.nextFloat();

In this Program, first, we take the input of the distance in Miles(mi) from the user as input using the Scanner Class in Java.

        //Miles to Kilometers conversion
        float km = (float)(1.609344 * miles);

Then, we convert the distance from Miles to Kilometers by multiplying 1.609344 to the distance in kilometers. Here, we have done the explicit type casting to float as the value after getting multiplied with 1.609344 is a double value so we converted the value to float explicitly.

        System.out.println("Distance in kilometers will be: "+km);

Then, we display the result using the System.out.println() function.

This is the Java Program to Convert Miles to Kilometers.

Read More : Java Program to Convert Kilometers to Miles

Conclusion

I hope after going through this post, you understand the Java Program to Convert Miles to Kilometers.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

1 thought on “Java Program to Convert Miles to Kilometers”

Leave a Comment

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