# C++ Program to Find Area, Perimeter of Square

« Previous Program Next Program »

In this article, you will learn and get code on finding the area and perimeter of a square in C++ programming. Here are the list of approaches used to create the program:

• Area of Square without using Function
• Area of Square using Function
• Perimeter of Square without using Function
• Perimeter of Square using Function
• Area and Perimeter of Square using Class and Object

Before starting the program, let's first see the formula to calculate area and perimeter of a square.

### Area of Square Formula

To find area of a square, use following formula:

`area = len*len`

len indicates the length of square. Because all the sides are of equal length in square, therefore get the length of side and square it. That will be the area.

### Perimeter of Square Formula

To find perimeter of a square, use following formula:

`perimeter = 4*len`

len indicates length. Now let's move on to the program.

## Find Area of Square

To calculate area of a square in C++ programming, you have to ask from user to enter the side length of square. With this side length, apply the formula as given above and initialize its value to a variable say area and print its value as output as shown here in the following program.

```#include<iostream>
using namespace std;
int main()
{
float len, area;
cout<<"Enter the Length of Square: ";
cin>>len;
area = len*len;
cout<<"\nArea = "<<area;
cout<<endl;
return 0;
}```

This program was build and run under Code::Blocks IDE. Here is the initial snapshot of sample run:

Now enter the value of length of square say 5 to find its area as shown in the final snapshot of the sample run given below:

### Area of Square using Function

This program does the same job as of previous one. The only difference is, this program finds area of square using a user-defined function named areaOfSquare(). It takes the length of square as its argument and returns the area value of square based on the length value passsed as its argument.

```#include<iostream>
using namespace std;
float areaOfSquare(float);
int main()
{
float len, area;
cout<<"Enter the Length of Square: ";
cin>>len;
area = areaOfSquare(len);
cout<<"\nArea = "<<area;
cout<<endl;
return 0;
}
float areaOfSquare(float len)
{
return (len*len);
}```

This program will produce the same output as of previous one.

## Find Perimeter of Square

Now let's create a program to finds perimeter of a square without using any function.

```#include<iostream>
using namespace std;
int main()
{
float len, per;
cout<<"Enter the Length of Square: ";
cin>>len;
per = 4*len;
cout<<"\nPerimeter = "<<per;
cout<<endl;
return 0;
}```

Here is its sample run:

Supply the length of any side of square to find and print its perimeter as shown in the following snapshot:

### Perimeter of Square using Function

This program also does the same job as of previous one. It uses a function named perOfSquare(), that calculates the perimeter of a square based on the length passed as its argument.

```#include<iostream>
using namespace std;
float perOfSquare(float);
int main()
{
float len;
cout<<"Enter the Length of Square: ";
cin>>len;
cout<<"\nPerimeter = "<<perOfSquare(len);
cout<<endl;
return 0;
}
float perOfSquare(float len)
{
return (4*len);
}```

## Find Area & Perimeter of Square using Class

This program finds both area and perimeter of a square using class and object.

```#include<iostream>
using namespace std;
class CodesCracker
{
private:
float len;
public:
void getData();
float findArea();
float findPerimeter();
};
void CodesCracker::getData()
{
cout<<"Enter the Length of Square: ";
cin>>len;
}
float CodesCracker::findArea()
{
return (len*len);
}
float CodesCracker::findPerimeter()
{
return (4*len);
}
int main()
{
CodesCracker c;
float area, per;
c.getData();
area = c.findArea();
per = c.findPerimeter();
cout<<"\nArea = "<<area;
cout<<"\nPerimeter = "<<per;
cout<<endl;
return 0;
}```

Here is its sample run with user input as 5: