C++ Program to Find Area and Perimeter of a Trapezoid
Introduction
A trapezoid (or trapezium in some regions) is a four-sided geometric figure with one pair of opposite sides parallel. It is widely used in architecture, engineering, and design. Calculating the area and perimeter of a trapezoid is a common task in geometry.
The area represents the total space enclosed by the trapezoid, while the perimeter measures the total length of its sides. In this article, we will discuss the formulas, implement them in C++, and explain the program with an example.
Formulas
The formulas for calculating the area and perimeter of a trapezoid are as follows:
-
Area (A): The formula to calculate the area of a trapezoid is:
A = 0.5 × (b1 + b2) × h
, where:b1
andb2
are the lengths of the two parallel sides (bases).h
is the height (the perpendicular distance between the two bases).
-
Perimeter (P): The perimeter of a trapezoid is the sum of all its sides:
P = b1 + b2 + a + c
, where:a
andc
are the lengths of the non-parallel sides.
Code
Here is the C++ implementation to compute the area and perimeter of a trapezoid:
#include <iostream> using namespace std; int main() { // Variables for the sides and height of the trapezoid double base1, base2, side1, side2, height; // Input the dimensions of the trapezoid cout << "Enter the length of the first base (b1): "; cin >> base1; cout << "Enter the length of the second base (b2): "; cin >> base2; cout << "Enter the length of the first non-parallel side (a): "; cin >> side1; cout << "Enter the length of the second non-parallel side (c): "; cin >> side2; cout << "Enter the height of the trapezoid (h): "; cin >> height; // Calculate the area double area = 0.5 * (base1 + base2) * height; // Calculate the perimeter double perimeter = base1 + base2 + side1 + side2; // Display the results cout << "Area of the trapezoid: " << area << endl; cout << "Perimeter of the trapezoid: " << perimeter << endl; return 0; }
Output
When the program is executed, it prompts the user to enter the dimensions of the trapezoid. Below is an example of the program in action:
Enter the length of the first base (b1): 10 Enter the length of the second base (b2): 8 Enter the length of the first non-parallel side (a): 5 Enter the length of the second non-parallel side (c): 6 Enter the height of the trapezoid (h): 7 Area of the trapezoid: 63 Perimeter of the trapezoid: 29
Explanation
Let us break down the program step-by-step:
-
Input:
The program first asks the user to input the lengths of the two bases (
b1
andb2
), the two non-parallel sides (a
andc
), and the height (h
). These values are essential for calculating both the area and the perimeter. -
Area Calculation:
The area is calculated using the formula:
A = 0.5 × (b1 + b2) × h
. This formula takes the average of the lengths of the two bases and multiplies it by the height. -
Perimeter Calculation:
The perimeter is calculated as:
P = b1 + b2 + a + c
. This formula sums the lengths of all four sides of the trapezoid. - Output: The calculated area and perimeter are displayed to the user in a clear and concise format.
This program provides a simple yet effective way to calculate the area and perimeter of a trapezoid. It demonstrates the practical use of formulas and arithmetic operations in geometry using C++.