C Program to Find Area and Perimeter of a Parallelogram
Introduction
A parallelogram is a four-sided geometric figure where opposite sides are equal and parallel to each other. It is a fundamental shape in geometry with many practical applications, ranging from architecture to engineering.
Calculating the area and perimeter of a parallelogram involves knowing the lengths of its base, side, and height. The base and height determine the area, while the perimeter depends on the base and side lengths.
In this tutorial, we will write a C program to compute both the area and perimeter of a parallelogram based on user input. We will also explain the underlying formulas and step-by-step logic of the program.
Formulas
1. Perimeter
The perimeter of a parallelogram is calculated as the sum of the lengths of all four sides. Since opposite sides are equal, the formula becomes:
Perimeter = 2 × (Base + Side)
2. Area
The area of a parallelogram is determined using its base and height. The height is the perpendicular distance from the base to the opposite side. The formula is:
Area = Base × Height
Here:
- Base: One of the parallel sides of the parallelogram.
- Side: The other side of the parallelogram, adjacent to the base.
- Height: The vertical distance between the two parallel sides.
Code
The following is the C program to calculate the area and perimeter of a parallelogram:
#include <stdio.h> int main() { double base, side, height, area, perimeter; // Input the base, side, and height of the parallelogram printf("Enter the base of the parallelogram: "); scanf("%lf", &base); printf("Enter the side of the parallelogram: "); scanf("%lf", &side); printf("Enter the height of the parallelogram: "); scanf("%lf", &height); // Calculate the perimeter perimeter = 2 * (base + side); // Calculate the area area = base * height; // Display the results printf("Perimeter of the parallelogram: %.2lf\n", perimeter); printf("Area of the parallelogram: %.2lf\n", area); return 0; }
Output
When the above program is executed, it prompts the user for the base, side, and height of the parallelogram, then calculates and displays the area and perimeter. Here's a sample output:
Enter the base of the parallelogram: 8 Enter the side of the parallelogram: 5 Enter the height of the parallelogram: 6 Perimeter of the parallelogram: 26.00 Area of the parallelogram: 48.00
Explanation
The program begins by taking three inputs from the user: the base, the side, and the height of the parallelogram. These values are stored in the variables base, side, and height.
The perimeter is calculated using the formula:
Perimeter = 2 × (Base + Side)
For example, if the base is 8 and the side is 5, the perimeter becomes:
2 × (8 + 5) = 26
The area is then computed using:
Area = Base × Height
For instance, if the base is 8 and the height is 6, the area becomes:
8 × 6 = 48
The program uses printf() to display the results with two decimal places for better readability. This ensures the values appear formatted and precise.