Calculating Area of a Trapezoid using Java

In this tutorial, we will learn how to calculate the area of a trapezoid in Java programming language.

A trapezoid is a 4-sided flat shape having straight sides that has a pair of opposite sides parallel.

A trapezoid:

  • has a pair of parallel sides
  • is an isosceles trapezoid when it has equal angles from a parallel side

The parallel sides are called the “bases“. The other two sides are known as “legs

The distance (at right angles) from one base to the other is called the height of the trapezoid

The Area of the trapezoid is given as the average of the two base lengths times the height:

In other words,

½ x (sum of lengths of parallel sides of the Trapezoid) x perpendicular distance between parallel sides

Area =((a+b)÷ 2 )× h

Java Program to Calculate Area of Trapezoid

import java.util.*;

public class Prog {
  public static void main(String[] args) {
      Scanner obj=new Scanner(;
    double area; 
    System.out.println(" Base of Trapezoid =  ");
    double a = obj.nextDouble();
    System.out.println("Second Base of Trapezoid =  ");
    double b = obj.nextDouble();
    System.out.println("Height of Trapezoid = ");
    double hgt = obj.nextDouble();

    area = 0.5 * (a+b) * hgt;
    System.out.println("Area = "+area);

We will prompt the user to enter the two values of the base of the trapezoid whose area is desired.

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

  • java.util package contains Scanner class

After creating an instance of Scanner class, we will use the nextDouble() method to read values of the two bases since they will be floating-point numbers. Also, we prompt the user to enter the height of the trapezoid.

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

Finally, display the value to the user.


Base of Trapezoid = 12
Second Base of Trapezoid = 6
Height of Trapezoid = 4
Area = 36.0

Also read,
How to draw various shapes in Java Swing

Leave a Reply

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