C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Unit Converter (km–mile, cm–inch, m–ft, kg–lb) - Java Program

import java.util.Scanner;

 

public class UnitConverter {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        int choice;

        double value;

 

        System.out.println("===== UNIT CONVERTER =====");

        System.out.println("1. Kilometer to Mile");

        System.out.println("2. Mile to Kilometer");

        System.out.println("3. Centimeter to Inch");

        System.out.println("4. Inch to Centimeter");

        System.out.println("5. Meter to Feet");

        System.out.println("6. Feet to Meter");

        System.out.println("7. Kilogram to Pound");

        System.out.println("8. Pound to Kilogram");

        System.out.println("9. Exit");

 

        System.out.print("Enter your choice: ");

        choice = sc.nextInt();

 

        if (choice >= 1 && choice <= 8) {

            System.out.print("Enter value: ");

            value = sc.nextDouble();

        } else {

            System.out.println("Exiting...");

            return;

        }

 

        switch (choice) {

            case 1:

                System.out.println("Kilometer → Mile: " + (value * 0.621371));

                break;

 

            case 2:

                System.out.println("Mile → Kilometer: " + (value * 1.60934));

                break;

 

            case 3:

                System.out.println("Centimeter → Inch: " + (value * 0.393701));

                break;

 

            case 4:

                System.out.println("Inch → Centimeter: " + (value * 2.54));

                break;

 

            case 5:

                System.out.println("Meter → Feet: " + (value * 3.28084));

                break;

 

            case 6:

                System.out.println("Feet → Meter: " + (value * 0.3048));

                break;

 

            case 7:

                System.out.println("Kilogram → Pound: " + (value * 2.20462));

                break;

 

            case 8:

                System.out.println("Pound → Kilogram: " + (value * 0.453592));

                break;

 

            default:

                System.out.println("Invalid choice!");

        }

    }

}

Output

 
OUTPUT :
===== UNIT CONVERTER =====
1. Kilometer to Mile
2. Mile to Kilometer
3. Centimeter to Inch
4. Inch to Centimeter
5. Meter to Feet
6. Feet to Meter
7. Kilogram to Pound
8. Pound to Kilogram
9. Exit
Enter your choice: 1
Enter value: 5

Kilometer → Mile: 3.106855

Explanation of the Program

Scanner for User Input

Scanner sc = new Scanner(System.in);

Display a Conversion Menu

Options include:

  • km ↔ mile
  • cm ↔ inch
  • m ↔ feet
  • kg ↔ pound

User selects a number to choose the conversion.

Take Input Value

If the choice is valid (1–8), program asks:

value = sc.nextDouble();

Perform Conversions Using Fixed Conversion Constants

Examples:

  • 1 km = 0.621371 miles
  • 1 mile = 1.60934 km
  • 1 cm = 0.393701 inch
  • 1 inch = 2.54 cm
  • 1 meter = 3.28084 feet
  • 1 foot = 0.3048 meter
  • 1 kg = 2.20462 pounds
  • 1 pound = 0.453592 kg

Switch Case Handles Each Conversion

Example:

System.out.println("Meter → Feet: " + (value * 3.28084));

Program Ends if User Selects Exit