Home  >  Article  >  Backend Development  >  Program to calculate the area and perimeter of a trapezoid

Program to calculate the area and perimeter of a trapezoid

王林
王林forward
2023-08-25 20:37:251616browse

Program to calculate the area and perimeter of a trapezoid

A trapezoid is a quadrilateral with at least one pair of sides parallel to each other. The area and perimeter of a trapezoid can be calculated using the following formula:

Perimeter = sum of all sides

Area = ½ x (sum of lengths of parallel sides) x vertical line between parallel sides Distance

Code Logic - The code will use 5 variables for all sides of the trapezoid and 1 variable for the vertical distance between two parallel sides. For the area variable calculation we will take a floating point variable which will be initialized with that value. To calculate it we will use the formula "½ x (sum of lengths of parallel sides) x perpendicular distance between parallel sides". For perimeter calculation, the variable is assigned the expression "(sum of all sides)".

The following code shows the program to calculate the area and perimeter of a trapezoid,

Example

Live demonstration

#include <stdio.h>
int main() {
   int a = 2 , b = 3 , c = 5 , d = 4, h = 5;
   float area, perimeter;
   printf("The sides of trapezium are %d , %d , %d , %d </p><p>", a,b,c,d);
   printf("Distance between two parallel sides is %d </p><p>", h);
   perimeter = a+b+c+d;
   area = 0.5 * (a + b) * h ;
   printf("Perimeter of the trapezium is %.1f</p><p>", perimeter);
   printf("Area of the trapezium is: %.3f", area);
   return 0;
}

Output

The sides of trapezium are 2 , 3 , 5 , 4
Distance between two parallel sides is 5
Perimeter of the trapezium is 14.0
Area of the trapezium is: 12.500

The above is the detailed content of Program to calculate the area and perimeter of a trapezoid. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete