Java Program to Find the Area of Trapezium

This is the Java Program to Find the Area of a Trapezium.

Problem Description

Given the dimensions of a trapezium, find out its area.

Problem Solution

The area of a trapezium can be calculated using the formula:

Area = (sum of parallel sides)*(height)/2.

Program/Source Code

Here is the source code of the Java Program to Find the Area of a Trapezium. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Find the Area of a Trapezium
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class AreaOfATrapezium {
  8.     // Function to find area of a trapezium
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         double l1,l2,height;
  12.         System.out.println("Enter the length of the two parallel sides 
  13.                                      and the height of the trapezium");
  14.         try{
  15.             l1=Double.parseDouble(br.readLine());
  16.             l2=Double.parseDouble(br.readLine());
  17.             height=Double.parseDouble(br.readLine());
  18.         }catch (Exception e){
  19.             System.out.println("An error occurred");
  20.             return;
  21.         }
  22.         if(l1<=0 || l2<=0 || height<=0){
  23.             System.out.println("Wrong Input");
  24.         }
  25.         System.out.println("Area = " + (l1+l2)*height/2 );
  26.     }
  27. }
Program Explanation

1. In function main(), first, the length of parallel sides and height of the trapezium is taken as input.
2. The condition if(l1<=0 || l2<=0 || height<=0) checks if the input is valid or not.
3. The print statement displays the area of the trapezium.

advertisement
advertisement

Time Complexity: O(1)

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the length of the two parallel sides and the height of the trapezium
13.765
23.555
8.0
Area = 149.28

Sanfoundry Global Education & Learning Series – Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.