Java Program to Convert Inch to Meter and Meter to Inch

In the previous article we have discussed about Java Program to Convert Inch to Kilometer and Kilometer to Inch

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

Java Program to Convert Inch to Meter and Meter to Inch

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

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

1 Meter =  39.3701 Inch
1 inch  = 0.0254 Meter

Formula to convert Inch to Meter.

Meter = Inch * 0.0254

Formula to convert Meter to Inch.

Inch  = Meter * 39.3701

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

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

Approach:

  • Declare Inch and Meter value.
  • Then convert Inch to Meter and Meter 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);
        //value of meter declared
        double meter = 1;
        // value of inch declared  
        double inch = 1;

        //converting inch to meter
        double m = inch * 0.0254;
        //converting meter to inch
        double in = meter * 39.3701;
        //printing result
        System.out.println("Value of "+meter+" meter in inch: "+ in);   
        System.out.println("Value of "+inch+" inch in meter: "+ m);   
   }
}
Output:

Value of 1.0 meter in inch: 39.3701
Value of 1.0 inch in meter: 0.0254

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

Approach:

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

        //converting inch to meter
        double m = inch * 0.0254;
        //converting meter to inch
        double in = meter * 39.3701;
        //printing result
        System.out.println("Value of "+meter+" meter in inch: "+ in);   
        System.out.println("Value of "+inch+" inch in meter: "+ m);   
   }
}
Output:

Enter value of meter: 
20
Enter value of inch: 
5400
Value of 20.0 meter in inch: 787.402
Value of 5400.0 inch in meter: 137.16

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

Approach:

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

Enter value of meter: 
1
Enter value of inch: 
1
Value of 1.0 meter in inch: 39.3701
Value of 1.0 inch in meter: 0.0254

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.

Related Java Programs: