
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find the area of a Trapezium
In this article, we will understand how to find the area of a trapezium using Java. The trapezium is a type of quadrilateral that has at least one pair of sides parallel to each other. The parallel sides of a trapezium are called bases and the non-parallel sides of a trapezium are called legs. It is also called a trapezoid.
The area of a trapezium is calculated using the formula ?
(height/2 * (side_1 + side_2). i.e. Area = ½ x (sum of the lengths of the parallel sides) x perpendicular distance between parallel sides
Below is a demonstration of the same. The area of a trapezium with the length of the parallel sides a and b and the height of the trapezium h is given by ?
Problem Statement
Write a program in Java to find the area of a Trapezium.
Input
side_1 = 5 side_2 = 6 height = 6
Output
Area of trapezium is: 33.0
Different approaches
Below are the different approaches to find the area of a Trapezium ?
Using user input
Following are the steps to find the area of a Trapezium using user input ?
- Import the Scanner class from java.util.
- Start by defining the main method.
- Declare three integer variables side_1, side_2, and height, and a float variable my_area.
- Initialize a Scanner object to read input values from the user.
- Ask the user to enter the lengths of both parallel sides and the height.
- Calculate the area by using the formula (height/2 * (side_1 + side_2)) and store it in my_area.
- Print the calculated area of the trapezium.
Example
Here, the input is entered by the user based on a prompt ?
import java.util.Scanner; public class AreaOfTrapezium { public static void main(String args[]){ // Declare variables int side_1, side_2, height; // Notify user of package imports and object creation System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined"); // Take input from user System.out.print("Enter the length of the first parallel side: "); side_1 = my_scanner.nextInt(); System.out.print("Enter the length of the second parallel side: "); side_2 = my_scanner.nextInt(); System.out.print("Enter the height of the trapezium: "); height = my_scanner.nextInt(); // Calculate area of trapezium using float for proper division float my_area = ((float)height / 2) * (side_1 + side_2); // Output the area System.out.println("The area of the trapezium is: " + my_area); // Close scanner my_scanner.close(); } }
Output
Required packages have been imported A reader object has been defined Enter the length of the first parallel side: 5 Enter the length of the first parallel side : 6 Enter the heigth of the trapezium : 6 The area of trapezium is: 33.0
Using predefined values
Following are the steps to find the area of a Trapezium using predefined value ?
- Start by defining the main method.
- Declare integer variables side_1, side_2, and height assign predefined values to them.
- Calculate the area using the formula (height/2 * (side_1 + side_2)).
- Print the values of side_1, side_2, height, and the calculated area.
Example
Here, the integer has been previously defined, and its value is accessed and displayed on the console ?
public class AreaOfTrapezium { public static void main(String args[]){ int side_1 = 5, side_2 = 6, height = 6; System.out.println("The sides and height of the trapezium is defined as " +side_1 + ", " + side_2 + " and " + height); float my_area = (height/2 * (side_1 + side_2)); System.out.println("The area of Trapezium is: " + my_area); } }
Output
The sides and height of the trapezium is defined as 5, 6 and 6 The area of Trapezium is: 33.0