# Java Program to Convert Inch to Kilometer and Kilometer to Inch

In the previous article we have discussed about Java Program to Convert Kilogram to Pound and Pound to Kilogram

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

## Java Program to Convert Inch to Kilometer and Kilometer to Inch

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

Generally Inch and Kilometer are used as unit in case of length/distance measurement.

1 Inch =  Kilometer
1 Kilometer =  Inch

Formula to convert Kilometer to Inch.

Inch = Kilometer * 39370.1

Formula to convert Inch to Kilometer.

Kilometer  = Inch * 2.54 * .00001 (OR)  Inch / 39370.1

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

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

Approach:

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

Program:

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

//converting inch to kilometer
double km = inch * 2.54 * .00001;
//converting kilometer to inch
double in = kilometer * 39370.1;
//printing result
System.out.println("Value of "+kilometer+" kilometer in inch: "+ in);
System.out.println("Value of "+inch+" inch in kilometer: "+ km);
}
}
Output:

Value of 1.0 kilometer in inch: 39370.1
Value of 1.0 inch in kilometer: 2.54E-5

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

Approach:

• Take user input of Inch and Kilometer value.
• Then convert Inch to Kilometer and Kilometer to Inch 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 kilometer
System.out.println("Enter value of kilometer: ");
double kilometer = sc.nextDouble();
//Taking the value input of double variable inch
System.out.println("Enter value of inch: ");
double inch = sc.nextDouble();

//converting inch to kilometer
double km = inch * 2.54 * .00001;
//converting kilometer to inch
double in = kilometer * 39370.1;
//printing result
System.out.println("Value of "+kilometer+" kilometer in inch: "+ in);
System.out.println("Value of "+inch+" inch in kilometer: "+ km);
}
}
Output:

Enter value of kilometer:
2
Enter value of inch:
20000
Value of 2.0 kilometer in inch: 78740.2
Value of 20000.0 inch in kilometer: 0.508


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

Approach:

• Take user input of Inch and Kilometer value.
• Call a user defined method by passing Inch and Kilometer value as parameter.
• Inside method convert Inch to Kilometer and Kilometer to Inch 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 kilometer
System.out.println("Enter value of kilometer: ");
double kilometer = sc.nextDouble();
//Taking the value input of double variable inch
System.out.println("Enter value of inch: ");
double inch = sc.nextDouble();
//calling user defined method convert()
convert(kilometer, inch);
}

//convert() method to convert kilometer to inch and vice versa
public static void convert(double kilometer, double inch)
{
//converting inch to kilometer
double km = inch / 39370.1;
//converting kilometer to inch
double in = kilometer * 39370.1;
//printing result
System.out.println("Value of "+kilometer+" kilometer in inch: "+ in);
System.out.println("Value of "+inch+" inch in kilometer: "+ km);
}
}
Output:

Enter value of kilometer:
2
Enter value of inch:
20000
Value of 2.0 kilometer in inch: 78740.2
Value of 20000.0 inch in kilometer: 0.507


The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Revise these:

1. Write a java program to convert centimeter to inch meter and kilometer?
2. Write a java program that reads a number in inches converts it to meters?
3. Write a program to convert kilometers to meters in java?
4. C program to convert centimeters to feet and inches?
5. C program to convert centimeters to inches?
6. Java program to convert inches to centimeters?
7. Java program to convert meter to kilometer?

Related Java Programs: