How to find Area of Trapezium using C++ programming

Trapezium:

There are two definitions for trapezium.
" A quadrilateral having one pair of parallel sides " or " A quadrilateral having no pair of parallel sides " known as trapezium. First definition is known outside the US and second one in known in inside the US.The two parallel sides are known as bases and the other two sides are known as legs of trapezium.

Area of Trapezium:

Area of trapezium is equal to sum of lengths of two bases of trapezium divided by two and then multiply by height. Bases are the two parallel sides. You can define the area of trapezium as that the length of mid segment multiplied by the height. Height is calculated as perpendicular distance between the two parallel sides.Let a and  b are two parallel sides of trapezium and h is height then area of trapezium can be written as 
area = (a + b ) * h / 2

Reference: Wikipedia

Explanation of Program code:

The first thing you should for is to include the library files like iostream or iostream.h in older versions.These files are necessary for in order to go for input and output streams in program code. By Input and output streams i mean cout and cin. Cout is for output stream and Cin is for input stream.Similarly you also have to add conio.h or if you don't want to use this file you can go for return 0; statement at the end of program code.

After this all you need is to define main function which is necessary part of every C++ program.Now next step is to look at the formula for area of Trapezium and think how many variables you need and also their types. Yes there are four variables you have to define. Two for bases , one for the height and one for to store the result which is area of trapezium.The type of variable is depends upon the values of bases and heights, if there are integers values then type is integer type and if there are decimal values then go for double type. Now rest is simple program , i used some statements for beauty of program.

Recommended Article: Read this articles for better understanding of program code.
Now the question is why i used do while loop. The answer is simple, to calculate area of trapezium for different values of bases and height. In order to avoid again and again execution of program code. For this i define char variable there in program code. I used comments in the program code somewhere so that program can be understand easily.

Program code:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
double a, b , height , area;  //  variables a, b height , area of data type double
 char redo;                         //   variable redo of type data type redo
cout<<"****************************************"<<endl; // display stars ***...
cout<<" Area of Trapezium "<<endl; // display " area of trapezium '
cout<<"****************************************"<<endl; // display ***.....

do
{
cout<<"Enter side 1  of trapezium "<<endl;     // display sentence inside inverted commas
cin>>a;                      //  stores value of side 1 in the variable a
cout<<endl;                //  go to next line

cout<<"Enter side 2 of trapezium "<<endl; // display statement inside the inverted commas
cin>>b;                                              // stores side 2 value
cout<<endl;                        
cout<<"Enter vertical height of trapezium "<<endl;
cin>>height;                                             // stores height value
cout<<endl<<endl;
area = (0.5 * (a + b) ) * height ;                          // variable area stores the value obtained form right side 
cout<<"Area of Trapezium is  =  "<<area;        // display statement inverted commas statement and area                                                                              //value
 cout<<endl;
 cout<<"***********************************"<<endl;  
 cout<<"Enter y or Y for next calculation  =   ";                    
 cin>>redo;
cout<<endl<<endl;

}while(redo == 'y' || redo == 'Y');
getch();
}

Output:
***************************************
                 Area of Trapezium
***************************************
Enter side 1 of trapezium
1
Enter side 2 of trapezium
2
Enter height of trapezium
4

Area of Trapezium is =  6
**************************************
Enter y or Y for next  calculation  =  

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.