Java Program to Convert Centimeter to Meter and Kilometer

In this article we will see how we can convert centimeter to meter and kilometer by using Java programming language.

Java Program to Convert Centimeter to Meter and Kilometer

Before jumping into the program let’s know the first basic information regarding unit values and relationship between centimeter, meter and kilometer.

1 meter = 100 centimeter

1 kilometer = 1000 meter = 100000 centimeter

So the formula to calculate meter and kilometer from centimeter is given below.

meter = centimeter/100

kilometer = centimeter/ 100000

Let’s see different ways to convert centimeter to meter and kilometer.

Method-1: Java Program to Convert Centimeter to Meter and Kilometer By Using Static Input Value

Approach:

  • Declare centimeter value.
  • Then convert centimeter to meter and kilometer by using formula.
  • meter = centimeter/10
    kilometer = centimeter/ 100000

Program:

public class Main 
{
   public static void main(String args[])
   {
        //double variable centimeter declared and value initialized
        double centimeter = 2500;
        //calculating meter value
        double meter  = centimeter/100; 
        //calculating kilometer value
        double kilometer = centimeter/ 100000;
        //printing result
        System.out.println("Value in meter is: "+ meter+" m");   
        System.out.println("Value in kilometer is: "+ kilometer+" km");   
   }
}
Output:

Value in meter is: 25.0 m
Value in kilometer is: 0.025 km

Method-2: Java Program to Convert Centimeter to Meter and Kilometer By Using User Input Value

Approach:

  • Take input of centimeter value.
  • Then convert centimeter to meter and kilometer by using formula.
  • meter = centimeter/10
    kilometer = centimeter/ 100000

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable centimeter 
        System.out.println("Enter value of centimeter: ");  
        double centimeter = sc.nextDouble();
        //calculating meter value
        double meter  = centimeter/100; 
        //calculating kilometer value
        double kilometer = centimeter/ 100000;
        //printing result
        System.out.println("Value in meter is: "+ meter+" m");   
        System.out.println("Value in kilometer is: "+ kilometer+" km");   
   }
}
Output:

Enter value of centimeter: 
3000
Value in meter is: 30.0 m
Value in kilometer is: 0.03 km

Method-3: Java Program to Convert Centimeter to Meter and Kilometer By Using User Defined Method

Approach:

  • Take input of centimeter value.
  • Call a user defined method by passing that centimeter value as parameter.
  • Inside method, convert centimeter to meter and kilometer by using formula.
  • meter = centimeter/10
    kilometer = centimeter/ 100000

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable centimeter 
        System.out.println("Enter value of centimeter: ");  
        double centimeter = sc.nextDouble();
        //calling user defined method convert()
        convert(centimeter);
   }
   
   public static void convert(double centimeter)
   {
        //calculating meter value
        double meter  = centimeter/100; 
        //calculating kilometer value
        double kilometer = centimeter/ 100000;
        //printing result
        System.out.println("Value in meter is: "+ meter+" m");   
        System.out.println("Value in kilometer is: "+ kilometer+" km");   
   }
}
Output:

Enter value of centimeter: 
400000
Value in meter is: 4000.0 m
Value in kilometer is: 4.0 km

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Leave a Comment