Java Program to Convert Hectare to Square Kilometer and Square Kilometer to Hectare

In the previous article, we have discussed about Java Program to Convert Hectare to Square Yard and Square Yard to Hectare

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

Java Program to Convert Hectare to Square Kilometer and Square Kilometer to Hectare

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

Generally Hectare and Square Kilometer are used as unit in case of land measurement.

1 Hectare =  0.01 Square Kilometer 
1 Square Kilometer = 100 Hectare

Formula to convert Hectare to Square Kilometer.

Square Kilometer  = Hectare * 0.01 (OR) Hectare/100

Formula to convert Square Kilometer to Hectare.

Hectare  = Square Kilometer  * 100

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

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

Approach:

  • Declare Hectare and Square Kilometer value.
  • Then convert Hectare to Square Kilometer and Square Kilometer 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);
        //value of square kilometer declared
        double squarekilometer = 1;
        //value of hectare declared
        double hectare = 1;
        
        //converting Hectare to square kilometer
        double sk = hectare * 0.01;
        //converting square yard to Hectare 
        double h = squarekilometer * 100;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square kilometer: "+ sk);   
        System.out.println("Value of "+squarekilometer+" square kilometer in hectare: "+ h);   
   }
}
Output:

Value of 1.0 hectare in square kilometer: 0.01
Value of 1.0 square kilometer in hectare: 100.0

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

Approach:

  • Take user input of Hectare and Square Kilometer value.
  • Then convert Hectare to Square Kilometer and Square Kilometer 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 squarekilometer
        System.out.println("Enter value of square kilometer: ");  
        double squarekilometer = 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 kilometer
        double sk = hectare * 0.01;
        //converting square yard to Hectare 
        double h = squarekilometer * 100;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square kilometer: "+ sk);   
        System.out.println("Value of "+squarekilometer+" square kilometer in hectare: "+ h);   
   }
}
Output:
Enter value of square kilometer: 
590
Enter value of hectare: 
7.1
Value of 7.1 hectare in square kilometer: 0.071
Value of 590.0 square kilometer in hectare: 59000.0

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

Approach:

  • Take user input of Hectare and Square Kilometer value.
  • Call a user defined method by passing Hectare and Square Kilometer value as parameter.
  • Inside method convert Hectare to Square Kilometer and Square Kilometer 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 squarekilometer
        System.out.println("Enter value of square kilometer: ");  
        double squarekilometer = 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(squarekilometer, hectare);
   }
   
   //convert() method to convert square kilometer to hectare and vice versa
   public static void convert(double squarekilometer, double hectare)
   {
        
        //converting Hectare to square kilometer
        double sk = hectare / 100;
        //converting square yard to Hectare 
        double h = squarekilometer * 100;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square kilometer: "+ sk);   
        System.out.println("Value of "+squarekilometer+" square kilometer in hectare: "+ h);   
   }
}
Output:

Enter value of square kilometer: 
5
Enter value of hectare: 
5
Value of 5.0 hectare in square kilometer: 0.05
Value of 5.0 square kilometer in hectare: 500.0

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: