C++ Program to Calculate Area of Square

In this post, we will learn how to calculate the area of a square using C++ Programming language.

Area of a square is calculated using the formula: Area = Side x Side.

We will calculate the area of a square using the following approaches:

  1. Using Standard Method
  2. Using Function

So, without any delay, let’s begin this tutorial.

C++ Program to Calculate Area of Square

// C++ Program to Calculate Area of Square
#include <iostream>
using namespace std;

int main(){
    int side, area;
    
    // Asking for input
    cout << "Enter the side of square: ";
    cin >> side;
    
    // Calculating area
    area = side * side;
    
    // Displaying output
    cout << "Area of square of side " << side << " is: " << area;
    return 0;
}

Output

Enter the side of square: 7
Area of square of side 7 is: 49

How Does This Program Work ?

    int side, area;

In this program, we have declared two integer data type variables named side and area.

    // Asking for input
    cout << "Enter the side of square: ";
    cin >> side;

The user is asked to enter the length of the side. This value gets stored in the side named variable.

    // Calculating area
    area = side * side;

Area is calculated by using the formula, area = side x side. The result gets stored in the area named variable.

    // Displaying output
    cout << "Area of square of side " << side << " is: " << area;

The area of the square is displayed on the screen using the cout statement.

C++ Program to Calculate Area of Square Using Function

// C++ Program to Calculate Area of Square Using Function
#include <iostream>
using namespace std;

int areaSquare(int x){
    return x * x;
}

int main(){
    int side, area;
    
    // Asking for input
    cout << "Enter the side of square: ";
    cin >> side;
    
    // Calling out function
    area = areaSquare(side);
    
    // Displaying output
    cout << "Area of the square is: " << area;
    return 0;
}

Output

Enter the side of square: 12
Area of the square is: 144

Conclusion

I hope after going through this post, you understand how to calculate the area of a square using C++ Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *