Signup/Sign In
PUBLISHED ON: APRIL 16, 2021

Java Program To Find The Area of a Trapezium

In this tutorial, we will learn how to calculate the area of a Trapezium in java. A trapezium is a 2D shape that falls under the category of quadrilaterals having one pair of parallel sides. The area of trapezium is the region covered by a trapezium in a two-dimensional plane. But before moving forward if you are not familiar with the concept of data types, then do check the article on Data types in Java.

Input: Enter the length of the parallel sides of the trapezium: 5

Enter the length of the parallel sides of the trapezium: 3

Enter the height of the trapezium: 4

Output: Area of the Trapezium: 16

Below is the pictorial representation of the same.

Two cases arise for the above problem:

Case 1: When the parallel sides and the height is given

Case 2: When all the sides are given.

Let us look at each of these cases separately.

Program 1: Java Program to Find the Area of a Trapezium

In this program, we will learn how to find the area of a trapezium using the base and height formula.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the value of the sides of the trapezium.
  4. Ask the user to initialize the variables.
  5. Declare another variable to store the height of the trapezium.
  6. Use the base and height formula to calculate the area.
  7. Display the result.
  8. Stop.

The below program demonstrates how to find the area of the trapezium.

//Java Program to Calculate the Area of a Trapezium
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user 
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the length of the parallel sides of the trapezium: ");
        double side1=sc.nextDouble();
        System.out.println("Enter the length of the parallel sides of the trapezium: ");
        double side2=sc.nextDouble();
        System.out.println("Enter the height of the trapezium: ");
        double height = sc.nextDouble();
        //Calculate the area
        double area=((side1+side2)*height)/2;
       if (side1 <= 0 || side2<=0)
            System.out.println("Length should be positve");
        else
            System.out.println("Area of trapezium = "+ area);
     }
}


Enter the length of the parallel sides of the trapezium: 6
Enter the length of the parallel sides of the trapezium: 8
Enter the height of the trapezium: 6
Area of trapezium = 42.0

Program 2: Java Program to Find the Area of a Trapezium

In this program, we will learn how to find the area of a trapezium when all the sides of the trapezium are given.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the value of the sides of the trapezium.
  4. Ask the user to initialize the variables.
  5. Declare another variable to store the semi-perimeter of the trapezium.
  6. Subtract the sides from the semi-perimeter of the trapezium.
  7. Calculate the square root of the above resultant.
  8. Now, use the formula to calculate the area of the trapezium.
  9. Display the result.
  10. Stop.

The below program demonstrates how to find the area of the trapezium.

//Java Program to Calculate the Area of a Trapezium
import java.util.Scanner;
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user 
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the length of the longer side of the trapezium: ");
        double a=sc.nextDouble();
        System.out.println("Enter the length of the shorter side of the trapezium: ");
        double b=sc.nextDouble();
        System.out.println("Enter the length of the non-parallel side of the trapezium: ");
        double c = sc.nextDouble();
         System.out.println("Enter the length of the non-parallel side of the trapezium: ");
        double d = sc.nextDouble();
        double s =(a+b+c+d)/2;
        double num=(s-a)*(s-b)*(s-b-c)*(s-b-d);
        double res=Math.sqrt(num);
        //Calculate the area
        double Area =(a+b)/(a-b)*res;
       if (a <= 0 || b<=0 || c<=0 || d<=0)
            System.out.println("Length should be positve");
        else
            System.out.println("Area of trapezium = "+ Area);
     }
}


Enter the length of the longer side of the trapezium: 14
Enter the length of the shorter side of the trapezium: 6
Enter the length of the non-parallel side of the trapezium: 5
Enter the length of the non-parallel side of the trapezium: 5
Area of trapezium = 30.0



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.