Java Program to Convert Centimeter to Inch and Inch to Centimeter

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

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

Java Program to Convert Centimeter to Inch and Inch to Centimeter

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

Both Centimeter and Inch are used as unit in case of length measurement. While centimeter is used for smaller length measurement, inch is used to measure little bigger length.

1 Inch = 2.54 Centimeter
1 Centimeter =  0.393701 Inch

Formula to convert Centimeter to Inch.

Inch = Centimeter / 2.54

Formula to convert Inch to Centimeter.

Centimeter  = Inch * 2.54

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

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

Approach:

  • Declare Centimeter and Inch value.
  • Then convert Centimeter to Inch and Inch 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 inch declared  
        double inch = 1;
        
        //converting centimeter to inch
        double in = centimeter / 2.54; 
        //converting inch to centimeter
        double cm = inch * 2.54;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in inch: "+ in);   
        System.out.println("Value of "+inch+" inch in centimeter: "+ cm);   
   }
}
Output:

Value of 1.0 centimeter in inch: 0.39370078740157477
Value of 1.0 inch in centimeter: 2.54

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

Approach:

  • Take user input of Centimeter and Inch value.
  • Then convert Centimeter to Inch and Inch 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 inch
        System.out.println("Enter value of inch: ");  
        double inch = sc.nextDouble();
        
        //converting centimeter to inch
        double in = centimeter / 2.54; 
        //converting inch to centimeter
        double cm = inch * 2.54;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in inch: "+ in);   
        System.out.println("Value of "+inch+" inch in centimeter: "+ cm);   
   }
}
Output:

Enter value of centimeter: 
30
Enter value of inch: 
12
Value of 30.0 centimeter in inch: 11.811023622047244
Value of 12.0 inch in centimeter: 30.48

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

Approach:

  • Take user input of Centimeter and Inch value.
  • Call a user defined method by passing Centimeter and Inch value as parameter.
  • Inside method convert Centimeter to Inch and Inch 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 inch
        System.out.println("Enter value of inch: ");  
        double inch = sc.nextDouble();
        //calling user defined method convert()
        convert(centimeter, inch);
   }
   
   //convert() method to convert centimeter to inch and vice versa
   public static void convert(double centimeter, double inch)
   {
        //converting centimeter to inch
        double in = centimeter / 2.54; 
        //converting inch to centimeter
        double cm = inch * 2.54;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in inch: "+ in);   
        System.out.println("Value of "+inch+" inch in centimeter: "+ cm);   
   }
}
Output:

Enter value of centimeter: 
55
Enter value of inch: 
15
Value of 55.0 centimeter in inch: 21.653543307086615
Value of 15.0 inch in centimeter: 38.1

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: