Java Program to convert Kilometer to Miles

Program

import java.util.*;
public class ConvertKilometerToMiles {
    public static void main(String[] args) {
        float kilometer, miles, conv_factor;
        System.out.println("Enter the kilometers to covert:");
        Scanner sc = new Scanner(System.in);
        kilometer = sc.nextFloat();
        conv_factor = 0.621371 f;
        miles = kilometer * conv_factor;
        System.out.println("Value in miles:\t" + miles);
    }
}

The program declares three float variables: kilometer,miles, and conv_factor. kilometer stores the value entered by the user in kilometers. miles stores the equivalent value in miles. conv_factor holds the conversion factor from kilometers to miles, which is approximately 0.621371 (1 kilometer = 0.621371 miles).

The program prompts the user to enter the value in kilometers using System.out.println("Enter the kilometers to convert:");. It then reads the input using a Scanner object sc, and stores the value in the kilometer variable using kilometer = sc.nextFloat();. The program calculates the equivalent value in miles by multiplying the input value in kilometer with the conversion factor (conv_factor). This calculation is performed using the expression miles = kilometer * conv_factor;.

Consider an example to illustrate step by step process to convert Kilometers to Miles

If the user enters 10 kilometers:

kilometer will be 10. The program calculates miles as 10 * 0.621371, which equals approximately 6.21371 miles. It then prints "Value in miles: 6.21371" to the console.

Output

Enter the kilometers to covert:
364
Value in miles: 226.17903