How to Find Area of Parallelogram using C++ Programming Language

Parallelogram:

A quadrilateral having four sides in which opposite sides are parallel to each other.The opposite side which are parallel to each other are equal of length. Also the angles opposite to each other in parallelogram are equal of measure.These conditions which are given below are necessary for definition of parallelogram otherwise it is not parallelogram.
  • Two pairs of Opposite sides are of equal length.
  • Two pairs of opposite angles are of equal measure
  • The diagonals must bisect each other

Area of Parallelogram:

Area of parallelogram is equal to base of parallelogram multiplied by the height of parallelogram. Now how to find it using c++ programming language.The answer is , its very easy : D. Lets start talk about it.There are two ways to find area of parallelogram, its up to you whether you go for first one or second one. First one is you defined values of base and height of parallelogram in you program code and just showed the output by using  formula of area. Second one is you asked the user to insert values which may be any one or you can say that user is going to solve the problem on calculator where he entered the values and get the result.

As you can see that Second option is more suitable because you are designing a program which help the users or from user point of view if you note , then its better to be programmed for any values of base and height for area of different parallelograms. It is possible that there are large number of parallelograms and you need to find area of each of them. So in order to find area of large number of parallelogram, second option is looking good.

Explanation of Program Code:

First of all you have to go for library files for input and output streams. After insertion of this you have to include the basic function, the main function where you can write the program code. It is the necessary function in C++ programming. After this just think over of this sentence " Area of Parallelogram "  .Whats comes to your mind, definitely you go for formula of area which is discussed before.

area = base * height

Now look at the formula, what you need lets start form variables , so how many variables you need , yes three variables ,first one which stores the value of base , second one for storing value of height and third one to store result value of area and what about the type of variable , so its up to question whether it has integers values or having decimal values or any other. Lets i will go for type double which has ability to deal with integers or decimal numbers.

double b , h , area;

There area three variables i defined , b for base value , h for height value and area for storing the result value after multiplication of base and height.In the program code given below cout and cin are the ouput and input streams respectively. I passed comments in the program code so that you can easily understand what i am going for.For comments i used green color.

Recommended Articles:  Read this article for better understanding of program code.

Now question is why i used do while loop in program code. Well there is no need of using this loop , if you are going for providing a facility to user to calculate area of parallelogram only once. Now if user want to calculate more than once, he will go for compile and run the code again and again which not looks good. So i used do while loop , now there is no need of executing code again and again. Execution of code required once by using this loop.

Program code:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
double b, h ,area;            // defining variables of type double
char redo;

do
{
cout<<"Enter base of parallelogram "<<endl;        // will display sentence as in inverted commas
cin>>b;                 // user will enter base value as i asked in the above statement and stored in variable b
cout<<endl;         // cursor will go into next line

cout<<"Enter vertical height of parallelogram "<<endl;   // will display sentence as in inverted commas
cin>>h;                  // user will enter height value as asked in above sentence ans stored in variable h
cout<<endl;           // cursor will go into next line

area = b * h;         //    multiplication result stored in variable area

cout<<"Area of Parallelogram is =  "<<area;           // will display area followed by the sentence in inverted                                                                                   //commas
cout<<endl;
cout<<"*********************************************"<<endl; // display stars
cout<<"type y or Y for next calculation = ";         // will display sentence in inverted commas
cin>>redo;                                                          //redo stores char value 
cout<<endl<<endl;                                              // just like pressing two enters in word document
}while(redo == 'y' || redo == 'Y');
getch();

}

Output: After compile and run , output will be like this.

Enter base of parallelogram
2
Enter height of parallelogram
2
Area of Parallelogram is
4
*******************************************
type 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.