C Program to Find Area and Perimeter of a Trapezoid

C Program to Find Area and Perimeter of a Trapezoid

C Program to Find Area and Perimeter of a Trapezoid

Introduction

The trapezoid, also known as a trapezium in some regions, is a four-sided geometric figure with one pair of parallel sides. Calculating the area and perimeter of a trapezoid is a common problem in geometry. This task becomes simpler with programming, where we can create reusable code to calculate these values based on user input.

In this article, we will explore how to calculate the area and perimeter of a trapezoid using a C program. We will discuss the formulae, provide the code, explain its functionality, and analyze the output.


Formula

To calculate the area and perimeter of a trapezoid, we use the following formulae:

  • Area: The formula for the area of a trapezoid is:
  • Area = 0.5 × (Base1 + Base2) × Height

    Here, Base1 and Base2 are the lengths of the two parallel sides, and Height is the perpendicular distance between them.

  • Perimeter: The formula for the perimeter is the sum of all sides:
  • Perimeter = Base1 + Base2 + Side1 + Side2

    Here, Side1 and Side2 are the lengths of the non-parallel sides.


Code

Below is the C program to calculate the area and perimeter of a trapezoid based on user-provided dimensions:

#include <stdio.h>

int main() {
    float base1, base2, height, side1, side2, area, perimeter;

    // Input dimensions of the trapezoid
    printf("Enter the length of Base1: ");
    scanf("%f", &base1);
    printf("Enter the length of Base2: ");
    scanf("%f", &base2);
    printf("Enter the height of the trapezoid: ");
    scanf("%f", &height);
    printf("Enter the length of Side1: ");
    scanf("%f", &side1);
    printf("Enter the length of Side2: ");
    scanf("%f", &side2);

    // Calculate area
    area = 0.5 * (base1 + base2) * height;

    // Calculate perimeter
    perimeter = base1 + base2 + side1 + side2;

    // Output the results
    printf("The area of the trapezoid is: %.2f\n", area);
    printf("The perimeter of the trapezoid is: %.2f\n", perimeter);

    return 0;
}
    

Output

Below is an example of how the program behaves when executed:

Enter the length of Base1: 8
Enter the length of Base2: 6
Enter the height of the trapezoid: 5
Enter the length of Side1: 4
Enter the length of Side2: 3
The area of the trapezoid is: 35.00
The perimeter of the trapezoid is: 21.00
    

Explanation

  • Inputs: The program begins by prompting the user to input the dimensions of the trapezoid, including the lengths of the two bases, the height, and the lengths of the two non-parallel sides.
  • Area Calculation: Using the formula for the area of a trapezoid, the program computes the area by adding the two bases, multiplying by the height, and dividing by 2.
  • Perimeter Calculation: The program sums all four sides of the trapezoid to determine the perimeter.
  • Outputs: Finally, the calculated area and perimeter are displayed with a precision of two decimal places.

This program is designed to handle floating-point inputs, making it versatile for cases where the dimensions are not whole numbers. Additionally, by separating the area and perimeter calculations, the program is modular and easy to modify if needed.