Find the Area of a Parallelogram in Java



In this article, we will understand how to find the area of a parallelogram using Java. A parallelogram has two pairs of parallel equal opposite sides. It has a base and a height which is the perpendicular distance between the base and its opposite parallel side.

The area of a parallelogram is calculated using the formula ?

base * height
i.e.
b x h

Problem Statement

Write a program in Java to find the area of a parallelogram. Below is a demonstration of the same ?

Input

Base : 6
Height : 8

Output

Area parallelogram is : 48

Different Approaches

Following are the different approaches to find the area of a parallelogram ?

Using user input

Following are the steps to find the area of a parallelogram ?

  • Import the Scanner class from java.util to read user input.
  • Declare integer variables base, height, and my_area to store the base, height, and calculated area.
  • Create a Scanner object to read input from the user.
  • Prompt the user to enter the base and height values.
  • Use the Scanner object to read the base and height values.
  • After that, we will calculate the area by multiplying the base and height (my_area = base * height).
  • Print the area of the parallelogram.

Example

Here, the input is being entered by the user based on a prompt using user input?

import java.util.Scanner;
public class AreaOfParallelogram{
   public static void main(String args[]){
      int base, height, my_area;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the value of parallelogram base : ");
      base = my_scanner.nextInt();
      System.out.print("Enter the value of parallelogram base : ");
      height = my_scanner.nextInt();
      my_area=base*height;
      System.out.println("The area of the parallelogram is : "+my_area);
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the value of parallelogram base : 6
Enter the value of parallelogram base : 8
The area of the parallelogram is : 48

Using predefined input

Following are the steps to find the area of a parallelogram using predefined input ?

  • First, we will declare integer variables base, height, and my_area.
  • Initialize the base with a value of 6 and the height with a value of 8.
  • Print the predefined values of base and height.
  • Calculate the area by multiplying the base and height (my_area = base * height).
  • Print the area of the parallelogram.

Example

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class AreaOfParallelogram{
   public static void main(String args[]){
      int base, height;
      base=6;
      height=8;
      System.out.println("The base and height values are defined as " +base +" and " +height);
      int my_area=base*height;
      System.out.println("The area of the parallelogram is : "+my_area);
   }
}

Output

The base and height values are defined as 6 and 8
The area of the parallelogram is : 48
Updated on: 2024-10-21T11:06:11+05:30

529 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements