In the previous article we have discussed Java Program to Find Perimeter of Rhombus
In this article we will discuss about how to find area and perimeter of Trapezium.
Program to Find Area and Perimeter of Trapezium
Before going into the program, let’s see how we find the area and perimeter of a trapezium.
Formula for area of Trapezium = 0.5 * (side1+side2) * height
Formula for perimeter of Trapezium = side1+side2+side3+side4
Let’s see different ways to do it.
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Method-1: Java Program to Find Area and Perimeter of Trapezium By User Input Value
In this method all the side lengths and height are taken as input from the user. Then area and perimeter are calculated using the formula.
Let’s see the program to understand it more clearly.
import java.util.*; public class Main { public static void main(String []args) { //Scanner Class instrance created Scanner sc=new Scanner(System.in); //Take side1,side2(both are the length of the parallel sides) and height input from the user System.out.println("Enter length(parallel sides of the trapezium) : "); double side1=sc.nextDouble(); System.out.println("Enter length(parallel sides of the trapezium) : "); double side2=sc.nextDouble(); System.out.println("Enter length(non-parallel sides of the trapezium) : "); double side3=sc.nextDouble(); System.out.println("Enter length(non-parallel sides of the trapezium) : "); double side4=sc.nextDouble(); System.out.println("Enter height of the trapezium: "); double height = sc.nextDouble(); //finding area using formula double area=((side1+side2)*height)/2; System.out.println("Area Of Trapezium : "+area); //finding perimeter using formula double perimeter=side1+side2+side3+side4; System.out.print("Perimeter Of Trapezium : "+perimeter); } }
Output: Enter length(parallel sides of the trapezium) : 5 Enter length(parallel sides of the trapezium) : 15 Enter length(non-parallel sides of the trapezium) : 11 Enter length(non-parallel sides of the trapezium) : 4 Enter height of the trapezium: 20 Area Of Trapezium : 200.0 Perimeter Of Trapezium : 35.0
Method-2: Java Program to Find Area and Perimeter of Trapezium By User Defined Method
In this method all the side lengths and height are taken as input from the user. Then these data are passed as parameter to an user defined methods to find area and perimeter of the trapezium. Where inside each method we have calculated area and perimeter using the formula.
Let’s see the program to understand it more clearly.
import java.util.*; public class Main { public static void main(String []args) { //Scanner Class instrance created Scanner sc=new Scanner(System.in); //Take side1,side2(both are the length of the parallel sides) and height input from the user System.out.println("Enter length(parallel sides of the trapezium) : "); double side1=sc.nextDouble(); System.out.println("Enter length(parallel sides of the trapezium) : "); double side2=sc.nextDouble(); System.out.println("Enter length(non-parallel sides of the trapezium) : "); double side3=sc.nextDouble(); System.out.println("Enter length(non-parallel sides of the trapezium) : "); double side4=sc.nextDouble(); System.out.println("Enter height of the trapezium: "); double height = sc.nextDouble(); //calling findArea() method to find area of trapezium findArea(side1, side2, height); //calling findPerimeter() method to find perimeter of trapezium findPerimeter(side1,side2,side3,side4); } //findArea() method public static void findArea(double side1, double side2, double height) { //finding area using formula double area=((side1+side2)*height)/2; System.out.println("Area Of Trapezium : "+area); } //findPerimeter() method public static void findPerimeter(double side1, double side2, double side3, double side4) { //finding perimeter using formula double perimeter=side1+side2+side3+side4; System.out.print("Perimeter Of Trapezium : "+perimeter); } }
Output: Enter length(parallel sides of the trapezium) : 5 Enter length(parallel sides of the trapezium) : 15 Enter length(non-parallel sides of the trapezium) : 11 Enter length(non-parallel sides of the trapezium) : 4 Enter height of the trapezium: 20 Area Of Trapezium : 200.0 Perimeter Of Trapezium : 35.0
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with
the available Java Program list is mandatory.
Related Java Programs: