# Java Program to Convert Centimeter to Kilometer and Kilometer to Centimeter

In the previous article, we have discussed about Java Program to Convert Centimeter to Foot and Foot to Centimeter

In this article we will see how to convert Centimeter to Kilometer and Kilometer to Centimeter by using Java programming language.

## Java Program to Convert Centimeter to Kilometer and Kilometer to Centimeter

Before jumping into the program let’s know the relationship between Centimeter and Kilometer and how we can convert Centimeter to Kilometer and vice versa.

Generally Centimeter and Kilometer are used as unit in case of distance measurement. Centimeter is mainly used for small distance measurement like geometrical measurements while kilometer is used to measure long distance measurement like road distance.

1 Centimeter = 1e-5 Kilometer
1 Kilometer =  100000 Centimeter

Formula to convert Centimeter to Kilometer.

Kilometer = Centimeter * 1e-5  OR  centimeter/100000

Formula to convert Kilometer to Centimeter.

Centimeter  = Kilometer * 100000

Let’s see different ways to convert Centimeter to Kilometer and Kilometer to Centimeter.

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

Approach:

• Declare Centimeter and Kilometer value.
• Then convert Centimeter to Kilometer and Kilometer to Centimeter by using the formula.
• Print result.

Program:

public class Main
{
public static void main(String args[])
{
//value of centimeter declared
double centimeter = 1;
//value of kilometer declared
double kilometer = 1;

//converting centimeter to kilometer
double km = centimeter * 1e-5;
//converting kilometer to centimeter
double cm = kilometer * 100000;
//printing result
System.out.println("Value of "+centimeter+" centimeter in kilometer: "+ km);
System.out.println("Value of "+kilometer+" kilometer in centimeter: "+ cm);
}
}
Output:

Value of 1.0 centimeter in kilometer: 1.0E-5
Value of 1.0 kilometer in centimeter: 100000.0

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

Approach:

• Take user input of Centimeter and Kilometer value.
• Then convert Centimeter to Kilometer and Kilometer to Centimeter by using the formula.
• Print result.

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();
//Taking the value input of double variable kilometer
System.out.println("Enter value of kilometer: ");
double kilometer = sc.nextDouble();

//converting centimeter to kilometer
double km = centimeter/100000;
//converting kilometer to centimeter
double cm = kilometer * 100000;
//printing result
System.out.println("Value of "+centimeter+" centimeter in kilometer: "+ km);
System.out.println("Value of "+kilometer+" kilometer in centimeter: "+ cm);
}
}
Output:

Enter value of centimeter:
56789
Enter value of kilometer:
56
Value of 56789.0 centimeter in kilometer: 0.56789
Value of 56.0 kilometer in centimeter: 5600000.0


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

Approach:

• Take user input of Centimeter and Kilometer value.
• Call a user defined method by passing Centimeter and Kilometer value as parameter.
• Inside method convert Centimeter to Kilometer and Foot to Kilometer by using the formula.
• Print result.

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();
//Taking the value input of double variable kilometer
System.out.println("Enter value of kilometer: ");
double kilometer = sc.nextDouble();
//calling user defined method convert()
convert(centimeter, kilometer);
}

//convert() method to convert centimeter to kilometer and vice versa
public static void convert(double centimeter, double kilometer)
{
//converting centimeter to kilometer
double km = centimeter * 1e-5;
//converting kilometer to centimeter
double cm = kilometer * 100000;
//printing result
System.out.println("Value of "+centimeter+" centimeter in kilometer: "+ km);
System.out.println("Value of "+kilometer+" kilometer in centimeter: "+ cm);
}
}
Output:

Enter value of centimeter:
500000
Enter value of kilometer:
5
Value of 500000.0 centimeter in kilometer: 5.0
Value of 5.0 kilometer in centimeter: 500000.0


Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: