Java program to convert centimetres to inches, meters and kilometres

Write a Java Program to convert centimetres to inches, meters and kilometres.

import java.util.Scanner;

public class MeasurementConversion {
    public static void main(String[] args) {

        // declare variables
        final double INCH = 0.394;
        final double METER = 0.01;
        final double KILOMETER = 0.00001;

        // Scanner class object to read input values
        Scanner in = new Scanner(System.in);

        // read length in cm from user
        System.out.print("Enter length in centimeters : ");
        double cm = in.nextDouble();

        // convert t inches, meters and kilometres
        double inch = cm * INCH;
        double m = cm * METER;
        double km = cm * KILOMETER;

        // display result
        System.out.println(cm + " cm = " + inch + " inches.");
        System.out.println(cm + " cm = " + m + " meters.");
        System.out.println(cm + " cm = " + km + " kilometers.");
    }
}

Output

Enter length in centimeters : 2560
2560.0 cm = 1008.6400000000001 inches.
2560.0 cm = 25.6 meters.
2560.0 cm = 0.0256 kilometers.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments