Calculating the area of a parallelogram in Java

In this tutorial, we will see how to calculate the area of parallelogram in Java.

A parallelogram is a four-sided plane rectilinear figure which has opposite sides parallel. In Euclidean geometry, a parallelogram is defined as a simple quadrilateral which has two pairs of parallel sides. The facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure.

Java program to calculate area of parallelogram

Area: The area of a parallelogram can be found by multiplying a base by the corresponding altitude. Any side can be considered a base. Choose any one you like, but the corresponding altitude must be used.

We define the altitude (or height) of a parallelogram as the perpendicular distance from the base to the opposite side (may have to be extended).

Formula: base × height

Java Code:

import java.util.*;
public class Prog
{
   public static void main(String args[]) 
    {   
       double area;
      Scanner obj= new Scanner(System.in);
    
      System.out.print("Height: ");
      double hgt= obj.nextDouble();
      System.out.print("\nBase:");
      double base= obj.nextDouble();
          
    area=base*hgt;
    System.out.println("\nArea is: " + area);      
   }
}

We will prompt the user to enter the value of the base(value for one of the sides) of the parallelogram.

For that purpose, we will use Scanner class to get user input.

  • java.util package contains Scanner class

Further, after we have created an instance of Scanner class, we will use the nextDouble() method. Since, these values floating point numbers, we store them in double type variable. Also, we prompt the user to enter the corresponding height of the parallelogram.




We use the given formula to calculate the area of parallelogram and store it in a variable “area”.

Finally, display the value to the user.

Output:

Height: 4
Base: 12
Area of the given Parallelogram is: 48.0

Also read,


Leave a Reply

Your email address will not be published. Required fields are marked *