This is the Java Program to Find the Area of a Trapezium.
Given the dimensions of a trapezium, find out its area.
The area of a trapezium can be calculated using the formula:
Area = (sum of parallel sides)*(height)/2.
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.
//Java Program to Find the Area of a Trapezium
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class AreaOfATrapezium {
// Function to find area of a trapezium
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double l1,l2,height;
System.out.println("Enter the length of the two parallel sides
and the height of the trapezium");
try{
l1=Double.parseDouble(br.readLine());
l2=Double.parseDouble(br.readLine());
height=Double.parseDouble(br.readLine());
}catch (Exception e){
System.out.println("An error occurred");
return;
}
if(l1<=0 || l2<=0 || height<=0){
System.out.println("Wrong Input");
}
System.out.println("Area = " + (l1+l2)*height/2 );
}
}
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.
Time Complexity: O(1)
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.
- Practice Programming MCQs
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Apply for Java Internship
- Check Programming Books