Compute the Area of a Trapezoid in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the area of a trapezoid using C programming. The lab covers the steps to read the two parallel sides and height of a trapezoid, and then calculate its area using the formula: Area = 0.5 _ (a+b) _ h. The program prompts the user to input the necessary values, and then computes and displays the area of the trapezoid.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435154{{"`Compute the Area of a Trapezoid in C`"}} c/variables -.-> lab-435154{{"`Compute the Area of a Trapezoid in C`"}} c/if_else -.-> lab-435154{{"`Compute the Area of a Trapezoid in C`"}} c/user_input -.-> lab-435154{{"`Compute the Area of a Trapezoid in C`"}} c/math_functions -.-> lab-435154{{"`Compute the Area of a Trapezoid in C`"}} end

Read Two Parallel Sides and Height

In this step, you will learn how to read the two parallel sides and height of a trapezoid to prepare for calculating its area in C programming.

First, let's create a new C file for our trapezoid area calculation:

cd ~/project
nano trapezoid_area.c

Now, add the following code to read the parallel sides and height:

#include <stdio.h>

int main() {
    float side1, side2, height;

    // Prompt user to input first parallel side
    printf("Enter the length of the first parallel side: ");
    scanf("%f", &side1);

    // Prompt user to input second parallel side
    printf("Enter the length of the second parallel side: ");
    scanf("%f", &side2);

    // Prompt user to input height
    printf("Enter the height of the trapezoid: ");
    scanf("%f", &height);

    // Print the input values to verify
    printf("First side: %.2f\n", side1);
    printf("Second side: %.2f\n", side2);
    printf("Height: %.2f\n", height);

    return 0;
}

Compile and run the program:

gcc trapezoid_area.c -o trapezoid_area
./trapezoid_area

Example output:

Enter the length of the first parallel side: 5
Enter the length of the second parallel side: 7
Enter the height of the trapezoid: 3
First side: 5.00
Second side: 7.00
Height: 3.00
Explanation
  • We use float to store decimal numbers for side lengths and height
  • printf() is used to prompt the user for input
  • scanf() reads the floating-point values entered by the user
  • We print the input values to verify they were correctly captured

Compute Area = 0.5*(a+b)*h

In this step, you will modify the previous C program to calculate the area of a trapezoid using the formula: Area = 0.5 _ (a+b) _ h.

Open the existing file and update the code:

cd ~/project
nano trapezoid_area.c

Replace the previous code with the following implementation:

#include <stdio.h>

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

    // Prompt user to input first parallel side
    printf("Enter the length of the first parallel side: ");
    scanf("%f", &side1);

    // Prompt user to input second parallel side
    printf("Enter the length of the second parallel side: ");
    scanf("%f", &side2);

    // Prompt user to input height
    printf("Enter the height of the trapezoid: ");
    scanf("%f", &height);

    // Calculate area using trapezoid area formula
    area = 0.5 * (side1 + side2) * height;

    // Print the calculated area
    printf("Trapezoid Area: %.2f\n", area);

    return 0;
}

Compile and run the program:

gcc trapezoid_area.c -o trapezoid_area
./trapezoid_area

Example output:

Enter the length of the first parallel side: 5
Enter the length of the second parallel side: 7
Enter the height of the trapezoid: 3
Trapezoid Area: 18.00
Explanation
  • We added a new area variable to store the calculated area
  • The formula 0.5 * (side1 + side2) * height calculates the trapezoid's area
  • printf() displays the calculated area with two decimal places
  • The calculation uses the standard trapezoid area formula: A = 0.5(a+b)h

Print the Area

In this step, you will enhance the trapezoid area calculation program by adding formatted output and error handling to improve user experience.

Open the existing file and update the code:

cd ~/project
nano trapezoid_area.c

Replace the previous code with the following implementation:

#include <stdio.h>

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

    // Input validation
    printf("Trapezoid Area Calculator\n");
    printf("-------------------------\n");

    // Prompt user to input first parallel side
    printf("Enter the length of the first parallel side: ");
    if (scanf("%f", &side1) != 1 || side1 <= 0) {
        printf("Error: Invalid input for first side.\n");
        return 1;
    }

    // Prompt user to input second parallel side
    printf("Enter the length of the second parallel side: ");
    if (scanf("%f", &side2) != 1 || side2 <= 0) {
        printf("Error: Invalid input for second side.\n");
        return 1;
    }

    // Prompt user to input height
    printf("Enter the height of the trapezoid: ");
    if (scanf("%f", &height) != 1 || height <= 0) {
        printf("Error: Invalid input for height.\n");
        return 1;
    }

    // Calculate area using trapezoid area formula
    area = 0.5 * (side1 + side2) * height;

    // Formatted output of results
    printf("\nTrapezoid Calculation Results:\n");
    printf("-----------------------------\n");
    printf("First Side:  %.2f\n", side1);
    printf("Second Side: %.2f\n", side2);
    printf("Height:      %.2f\n", height);
    printf("Area:        %.2f square units\n", area);

    return 0;
}

Compile and run the program:

gcc trapezoid_area.c -o trapezoid_area
./trapezoid_area

Example output:

Trapezoid Area Calculator
-------------------------
Enter the length of the first parallel side: 5
Enter the length of the second parallel side: 7
Enter the height of the trapezoid: 3

Trapezoid Calculation Results:
-----------------------------
First Side:  5.00
Second Side: 7.00
Height:      3.00
Area:        18.00 square units
Explanation
  • Added input validation to prevent invalid inputs
  • Improved output formatting with headers and clear labels
  • Added error handling for incorrect input types
  • Included descriptive output with all input parameters and calculated area
  • Used %.2f to display floating-point numbers with two decimal places

Summary

In this lab, you first learned how to read the two parallel sides and height of a trapezoid using the printf() and scanf() functions. You then calculated the area of the trapezoid using the formula: Area = 0.5 _ (a+b) _ h, where a and b are the parallel sides, and h is the height. Finally, you printed the calculated area to the console.

Other C Tutorials you may like