# C++ Program to Calculate Square of a Number

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

The square of a number is the product of the number with the number itself. For example: the square of 6 is 6 x 6 = 36.

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

1. Using Standard Method
2. Using Pow() Function
3. Using Function

So, without further ado, let’s begin this post.

Contents

## C++ Program to Calculate Square of a Number

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

int main(){
int n, square;

cout << "Enter a number: ";
cin >> n;

// Calculating square
square = n * n;

// Displaying output
cout << "Square of " << n << " is: " << square;

return 0;
}
```

Output

``````Enter a number: 25
Square of 25 is: 625
``````

## How Does This Program Work ?

```    int n, square;
```

In this program, we have declared two integer data type variables named n and square.

```    // Asking for input
cout << "Enter a number: ";
cin >> n;
```

The user is asked to enter a number. This number gets stored in the ‘n’ named variable.

```    // Calculating square
square = n * n;
```

Square is calculated by multiplying the same number by itself.

```    // Displaying output
cout << "Square of " << n << " is: " << square;
```

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

## C++ Program to Calculate Square of a Number Using Pow() Function

```// C++ Program to Calculate Square of a Number Using Pow() Function
#include <iostream>
#include <cmath>
using namespace std;

int main(){
int n, square;

cout << "Enter a number: ";
cin >> n;

// Calculating square
square = pow(n, 2);

// Displaying output
cout << "Square of " << n << " is: " << square;

return 0;
}
```

Output

``````Enter a number: 9
Square of 9 is: 81
``````

## C++ Program to Calculate Square of a Number Using Function

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

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

int main(){
int n, square;

cout << "Enter an integer: ";
cin >> n;

// Calling out the custom function
square = calSquare(n);

// Displaying output
cout << "The square of " << n << " is: " << square;
return 0;
}
```

Output

``````Enter an integer: 21
The square of 21 is: 441
``````

## Conclusion

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

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to solve your doubts.