How to Find Area of Square using C++ Programming

Area of Square:

The area of square is equal to multiplication of length of any two sides of square.Because all sides of square area equal in length so you can take any two sides length.If A represents the area and a represents the side length then the formula for area of square is as follows.
A =  a * a 

Explanation of Program code:

First of all you have to include library files for input and output streams. cout and cin are output and input streams respectively. Secondly you have to insert main function which is the necessary function in order to do C++ programming.Program code is written inside this function.You can define other functions as well but in order to use those functions you need main function. The syntax of that function is as follows

int main (){}

int represents the data type of main function which is integer type.Program code is written inside the curly braces as shown above like this {}. At the end of program code you have to write the return statement but inside the curly braces of main function.It means now there are no more statements for execution or compiling.

return 0 ;

Now look at the formula of area of square. How many variables you require,it seems that you require three variables but as the length of sides of square are equal in length so you can use one side.So two variables. One for the length of any side and other for area. Data type of these variables depends upon the length of sides of square if there are integer values then integer type and in case of decimal values then double type.

Recommended Articles: Read this article to understand why i used this.
In order to avoid again and again execution, it means if you have large number of squares and you need to calculate area of all of them then you compile the code again and again.But while loop helps in that way, i define variable redo of type character and used the statements "Enter y for next calculation ". If you dont want to use this loop then remove this loop.

Program code:
#include<iostream>
using namespace std;
int main()
{
double side, area;
char redo; 
do
{
cout<<"Enter any side value of square "<<endl;
cin>>side;
cout<<endl<<endl;

area = side * side ;

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

}

Output:
Enter any side value of square
2

Area of Square 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.