Write a C program that computes the area of a trapezium.

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>

int main(int argc, char ** argv) {

    float area, a, b, h;
    printf("Enter the height of the trapezium: ");
    scanf("%f", &h);
    printf("Enter length of first side: ");
    scanf("%f", &a);
    printf("Enter length of second side: ");
    scanf("%f", &b);
    area = 0.5 * (a + b) * h;
    printf("Area is %.2f\n", area);

    return 0;

When you compile and execute the above program it produces the following result on Linux:

Enter the height of the trapezium: 4
Enter length of first side: 2
Enter length of second side: 2
Area is 8.00

Brief Explanation

  • The program starts by requesting the user to input the following: height of trapezium, length of first and second side.
  • Using the formula 0.5(a+b)h, the area of the trapezium is computed.
  • Using printf function, the area is printed to the stdout.

Add comment

Security code