C++ Program to Calculate Square of a Number

Howdy readers, today you will learn how to write a C++ program to calculate the square of a number.

The number that is obtained by multiplying an integer by itself is known as a square number. Suppose, ‘n’ is an integer, then the square of ‘n’ is (n x n) or n2. For example, in 7 x 7 = 49, 49 is a square number.

We will use the following approaches to find the square of a number:

1. Using (*) Arithmetic Operator
2. Using Pow
3. Using Function

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

C++ Program to Calculate Square of a Number

C++ Program

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

int main(){
int num, square;

// Taking input
cout << "Enter an integer: ";
cin >> num;

// Square of a number
square = num * num;

// Display result
cout << "The square of " << num << " is: " << square;

return 0;
}
```

Output

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

Explanation

```    int num, square;
```

We have declared two int data type variables named `num` and `square`.

```    cout << "Enter an integer: ";
cin >> num;
```

The input is taken from the user. The entered number gets stored in the `num` named variable.

```    square = num * num;
```

The square is computed by multiplying the num by num using the (*) arithmetic operator. The result obtained gets stored in the `square` named variable.

```    cout << "The square of " << num << " is: " << square;
```

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

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

C++ Program

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

int main(){
double num, sqr;

// Taking input
cout << "Enter a number: ";
cin >> num;

// Square using pow
sqr = pow(num, 2);

// Display output
cout << "The square of the entered integer is: " << sqr;

return 0;
}
```

Output

```Enter a number: 9
The square of the entered integer is: 81
```

Explanation

```    sqr = pow(num, 2);
```

The pow() function returns the value of a base raised to the power of another exponent. It is defined in the cmath header file.

pow(base, exponent) = baseexponent

Thus, we get pow(num, 2) = (num)2 = square.

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

C++ Program

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

int Square(int a){
return a * a;
}

int main(){
int num, sqr;

// Taking input
cout << "Enter a number: ";
cin >> num;

// Square using function
sqr = Square(num);

// Display output
cout << "Square of " << num << " is: " << sqr;

return 0;
}
```

Output

```Enter a number: 36
Square of 36 is: 1296
```

Explanation

```int Square(int a){
return a * a;
}
```

We have defined a function named `Square` which computes and returns the square of a number.

```    sqr = Square(num);
```

The input taken from the user is passed to the user-defined function argument. The value returned from the function gets stored in the sqr variable.

```     cout << "Square of " << num << " is: " << sqr;
```

Then, the square is printed on the screen using the cout statement.

Conclusion

Today you have learned how to write a C++ program to calculate the square of a number.

If you have any queries related to the tutorial, comment down your questions in the comment section.