Java Program to Convert Hectare to Square Inch and Square Inch to Hectare

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

Java Program to Convert Hectare to Square Inch and Square Inch to Hectare

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

Generally Hectare is used as unit in case of measuring medium to large field/land while Square inch is used as unit in case of measuring small to medium field/land.

1 Hectare = 1.55e+7 Square Inch
1 Square Inch = 6.4516e-8 Hectare

Formula to convert Hectare to Square Inch.

Square Inch = Hectare * 1.55e+7

Formula to convert Square Inch to Hectare.

Hectare  = Square Inch / 1.55e+7

Let’s see different ways to convert Hectare to Square Inch and Square Inch to Hectare.

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

Approach:

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

Program:

public class Main 
{
   public static void main(String args[])
   {
        //value of square inch declared  
        double squareInch = 1;
        //value of hectare declared
        double hectare = 1;
        
        //converting Hectare to square inch
        double si = hectare * 1.55e+7;
        //converting square inch to Hectare 
        double h = squareInch / 1.55e+7;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square inch: "+ si);   
        System.out.println("Value of "+squareInch+" square inch in hectare: "+ h);   
   }
}
Output:

Value of 1.0 hectare in square inch: 1.55E7
Value of 1.0 square inch in hectare: 6.451612903225807E-8

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

Approach:

  • Take user input of Hectare and Square Inch value.
  • Then convert Hectare to Square Inch and Square Inch to Hectare 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 squareInch
        System.out.println("Enter value of square inch: ");  
        double squareInch = sc.nextDouble();
        //Taking the value input of double variable hectare
        System.out.println("Enter value of hectare: ");  
        double hectare = sc.nextDouble();
        
        //converting Hectare to square inch
        double si = hectare * 1.55e+7;
        //converting square inch to Hectare 
        double h = squareInch / 1.55e+7;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square inch: "+ si);   
        System.out.println("Value of "+squareInch+" square inch in hectare: "+ h);   
   }
}
Output:

Enter value of square inch: 
10000000
Enter value of hectare: 
0.5
Value of 0.5 hectare in square inch: 7750000.0
Value of 1.0E7 square inch in hectare: 0.6451612903225806

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

Approach:

  • Take user input of Hectare and Square Inch value.
  • Call a user defined method by passing Hectare and Square Inch value as parameter.
  • Inside method convert Hectare to Square Inch and Square Inch to Hectare 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 squareInch
        System.out.println("Enter value of square inch: ");  
        double squareInch = sc.nextDouble();
        //Taking the value input of double variable hectare
        System.out.println("Enter value of hectare: ");  
        double hectare = sc.nextDouble();
        //calling user defined method convert()
        convert(squareInch, hectare);
   }
   
   //convert() method to convert square inch to hectare and vice versa
   public static void convert(double squareInch, double hectare)
   {
        //converting Hectare to square inch
        double si = hectare * 1.55e+7;
        //converting square inch to Hectare 
        double h = squareInch / 1.55e+7;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square inch: "+ si);   
        System.out.println("Value of "+squareInch+" square inch in hectare: "+ h);   
   }
}
Output:

Enter value of square inch: 
999999
Enter value of hectare: 
2
Value of 2.0 hectare in square inch: 3.1E7
Value of 999999.0 square inch in hectare: 0.06451606451612903

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: