C++ Program to Find Area and Perimeter of a Parallelogram
Introduction
A parallelogram is a four-sided polygon with opposite sides that are equal in length and parallel to each other. This geometric shape is widely used in mathematics and engineering due to its unique properties. The area of a parallelogram represents the space it encloses, while the perimeter measures the total length of its boundary.
This article focuses on developing a C++ program to compute the area and perimeter of a parallelogram. We will use simple formulas and explain each part of the code for better understanding.
Formulas
To calculate the area and perimeter of a parallelogram, the following formulas are used:
-
Area: The formula for the area of a parallelogram is:
Area = base * height
. Here,base
is the length of the bottom edge, andheight
is the perpendicular distance between the opposite sides. -
Perimeter: The formula for the perimeter of a parallelogram is:
Perimeter = 2 * (base + side)
. In this formula,side
is the length of the adjacent side.
Code
#include <iostream> using namespace std; int main() { // Declare variables for base, height, and side double base, height, side, area, perimeter; // Input base, height, and side from the user cout << "Enter the base of the parallelogram: "; cin >> base; cout << "Enter the height of the parallelogram: "; cin >> height; cout << "Enter the length of the side of the parallelogram: "; cin >> side; // Calculate the area area = base * height; // Calculate the perimeter perimeter = 2 * (base + side); // Display the results cout << "Area of the parallelogram: " << area << endl; cout << "Perimeter of the parallelogram: " << perimeter << endl; return 0; }
Output
Here is an example of the output you will see when running the program:
Enter the base of the parallelogram: 12 Enter the height of the parallelogram: 8 Enter the length of the side of the parallelogram: 10 Area of the parallelogram: 96 Perimeter of the parallelogram: 44
Explanation
The program is straightforward and interactive, guiding the user to input the required dimensions of the parallelogram: base
, height
, and side
. After receiving these inputs:
-
The area is calculated using the formula:
Area = base * height
. This provides the total enclosed space of the parallelogram. -
The perimeter is calculated using:
Perimeter = 2 * (base + side)
, representing the total boundary length of the shape.
The program uses double
data types to handle both integer and decimal inputs for accurate results. It also employs the iostream
library for input and output operations.