C++ program to calculate the area and perimeter of the square

C++ program to calculate the area and perimeter of the square
You will learn about C++ program to calculate the area of square using a function and without function.

For the better understanding of this program you have to know more about the following C++ programming topic(s) :

C++ function

 

 The formula is to calculate the area of a square is:

Area = (side length) * (side length) 

Example#1.0: C++ program to calculate the area and perimeter  of square without function:

#include <iostream>
using namespace std;
int main()
{
float l, a, p;
cout << "Enter the side lenght of square\n";
cin >> l;
a = l * l;
p = 4.0 * l;
cout << "Area of Square is \t\t" << a << endl;
cout << "Perimeter of Square is \t" << p << endl;
return 0;
}

Output

Enter the side lenght of square
25.5
Area of Square is               650.25
Perimeter of Square is          102

 

Example#1.1: C++ program to calculate the area and perimeter of square using function:

#include <iostream>
using namespace std;
//function prototype
float squareArea(float l);
float squarePerimeter(float l);
//function main begins program execution
int main()
{
float l;
cout << "Enter the side lenght of square\n";
cin >> l;
cout << "Area of Square is \t\t" << squareArea(l) << endl;
cout << "Perimeter of Square is \t" << squarePerimeter(l) << endl;
//function call
return 0;
}
//implementation of squareArea function
float squareArea(float l)
{
return (l * l);
}
//implementation of squarePerimeter function
float squarePerimeter(float l)
{
return (4.0 * l);
}

Output

 Enter the side lenght of square
25.5
Area of Square is               650.25
Perimeter of Square is          102

Press Enter to return to Quincy...

Post a Comment

Thank you for vising

أحدث أقدم