How to Find Area of Rectangle using C++ Programming

Area of Rectangle:

The area of rectangle is equal to multiplication of width of rectangle and height of rectangle.In rectangle opposite sides area equal in length. If A represents area of rectangle, w represents the width and h represents the height of rectangle then the formula is given as follows
A = w * h

Explanation of Program Code:

First of all you have to enter the library files for input and output streams. cout and cin are output and input streams respectively.Secondly you have to enter the main function which is necessary for every C++ program.The syntax for main function is as follows

int main(){}

The program code is written inside the curly braces as shown above like this {}.Then you have to insert or write the return statement at the end of program code but inside the curly braces of main function which tells the compiler that there is no more statement to compile.

return 0;

Now look at the formula of area of rectangle and think how many variables you need to define , definitely three variables.One for to store the value of width , second to store the value of rectangle and third to store the value of area.Data type may be integer type in case of integer values and double in case of decimal values.

Recommended Articles: Read this article in order to understand why i used this.
There is no need of using this loop, remove this loop if you don't want to use this loop.The only purpose is to avoid again and again execution of program code.

Program code:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
double width , height , area;
char redo;
do
{
cout<<"Enter width of Rectangle "<<endl;
cin>>width;
cout<<endl;
cout<<"Enter height of Rectangle "<<endl;
cin>>height;
cout<<endl<<endl;

area = width * height ;

cout<<"Area of Rectangle is  =  "<<area;
cout<<endl<<"**********************************"<<endl;
cout<<"Enter y for next calculation = ";
cin>>redo;
cout<<endl;
}while(redo == 'y');
return 0;

}
Output:
Enter width of Rectangle
2
Enter height of Rectangle
2

Area of Rectangle is = 4;
**************************
Enter 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.