Home > Backend Development > C++ > body text

Program to calculate the area and perimeter of a trapezoid

王林
Release: 2023-08-25 20:37:25
forward
1656 people have browsed it

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;
}
Copy after login

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template