Program to find area of a trapezoid in C++

In this tutorial, we will learn how to calculate or find the area of a trapezoid in C++.

A trapezoid is a quadrilateral with two parallel sides and the other two sides which are not parallel. The parallel sides are called bases and the other two sides are called legs. The figure of a trapezoid is given as

find the area of a trapezoid in C++

 

This figure is denoted as figure1.

 

Here AB||DC and AB and DC are called as bases and AD and BC are called as legs. ‘h’ is called height of the trapezoid.

Area of a trapezoid :1/2*(a+b)*h.

a=base1 of the trapezoid

b=base2 of the trapezoid

h=height of the trapezoid

Find the area of a trapezoid in C++

Let us consider a trapezoid and let b1,b2 and h be the bases and height of the trapezoid. To declare and initialize at compile time the syntax is as follows

Syntax:

float b1=a;

float b2=b;

float h=h;

float area=1/2*(a+b)*h;

C++ code for the above syntax is:

int main()
{
 float b1=12;
 float b2=13.5;
 float h=6.8;
 float A;
 /* A=1/2*(b1+b2)*h; */
 A=(b1+b2)*h/2;
 cout<<"Area of trapezoid is "<<A<<"\n";
 return 0;
}

Output:

Area of trapezoid is 86.70000

To declare and initialize at run time  the syntax is as follows

Syntax:

float b1,b2,h,a;

cin>>b1;

cin>>b2;

cin>>h;

a=1/2*(b1+b2)*h;

C++ code for the above syntax  is:

int main()
{ 
 float b1,b2,h,a;
 cout<<"enter base 1 of trapezoid :";
 cin>>b1;
 cout<<"\n enter base 2 of trapezoid :";
 cin>>b2;
 cout<<"\n enter height of trapezoid :";
 cin>>h;
 a=1/2*(b1+b2)*h;
 cout<<"\n area of trapezoid is<<a;
 return 0;
}

Output:

enter base 1 of trapezoid:12

enter base 2 of trapezoid:13.5

enter height of trapezoid:6.8

area of trapezoid is 86.700000.

 

Formula for height of a trapezoid

Here if the height of the trapezoid is not given and legs of the trapezoid are given as l1 and l2 ,then by using pythagorean theorem we can find the height . The Pythagorean theorem is explained using the below figure.

 

rightangled triangle

 

 

From above figure1  consider  F is a point on AB. DF is the perpendicular of CD.

Now x=AF-CD

Now to find the height we use

Square of(l1)=square of(x) +square of (x).

where x=additional length of long parallel side(i.e base of the right-angled triangle formed with the corners A,D)

l1=hypotenuse of the right-angled triangle(leg of the trapezoid).

You may also read:

3 responses to “Program to find area of a trapezoid in C++”

  1. ahmed says:

    the output comes “area of trapezoid is=0” like what did i do wrong i literary copy pasted the code and i refreshed my page and i did it on another page its all the same

    • Tabassum Tanveer says:

      Just trace the code. Check if any errors and please make sure you mentioned all the header files which I didn’t in my code.

      • Rohith Venkat says:

        In line 10 of the code, the 1/2 evaluates to 0 all the time because it is integer division, making the output 0 for any input.
        Please change it to (b1+b2)*h/2, then it’ll work well.

Leave a Reply

Your email address will not be published.