Table of Contents
Example
Output
Home Backend Development C++ Program to calculate the area and perimeter of a trapezoid

Program to calculate the area and perimeter of a trapezoid

Aug 25, 2023 pm 08:37 PM
perimeter trapezoid area

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

What are the types of values ​​returned by c language functions? What determines the return value?

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

Where is the return value of the c language function stored in memory?

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

How does the C Standard Template Library (STL) work?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

See all articles