# 3 Working Ways To Calculate Square In C++

## What is square of a number?

If a given number is multiplied by itself then it’s called as a square of a number. For example 2*2 =4, 3*3=9, 4*4=16.  When the square is again multiplied by a number then it’s called the cube of a number.

In this article, we are going to learn different working methods to calculate the square in C++ programming language.

Following are the 3 working methods with which we can calculate the square of a number.

1. Using pow() function
2. By multiplying given number by itself
3. By addition using for loop

So don’t waste your time and start understanding these three methods one by one.

## Methods to calculate square in C++

### Calculate square in c++ using pow() function:

This is one of the easiest ways of calculating the square of a number.

cmath library file contains the inbuilt method or function called pow() that you can directly use to calculate the square of a given number.

Syntax of pow() function:

double pow(double base_value, double power_value)

Explanation: pow() function takes two input arguments i.e. base number whose square needs to be calculated and power value. Here we will give the power value as 2 as we have to calculate the square of a number. To calculate the cube of the value, enter 3 in place of power value.

Following is the example of using the pow() function:

``````#include<iostream>
#include<cmath>
using namespace std;

int main()
{
double m = 5;
double n = 2;
double result = pow(m,n);

cout<<"Square of a number using pow function is: "<< result <<endl;

}``````

Output:

``Square of a number using pow function is: 25``

Note: You must include cmath library to use the pow() function to calculate the square of a number

### Calculate square in c++ by multiplying a number by itself:

This is also one of the most widely used and simplest methods to calculate the square of a given number. As per the definition of square number, we have to multiply a given number by itself using multiplication symbol i.e. * as shown below:

``````#include<iostream>

using namespace std;

double getSquareOfNumber(double inputNumber);

int main()
{
double m = 5;

double result = getSquareOfNumber(m);

cout<<"Square of a number by multiplication is: "<< result <<endl;

}

double getSquareOfNumber(double inputNumber)
{
double result;

result = inputNumber*inputNumber;

return result;
}``````

Output

``Square of a number by multiplication is: 25``

### Calculate square in c++ by addition using for loop:

This is also one of the method using which you can calculate the square of a given number. But it’s very rarely used. But here we will learn it for understanding purpose.

The square of any number is nothing but the addition of that number into itself for the same number of times. Let’s understand this approach of calculating the square of the number using an example.

5+5+5+5+5=25

As you can see in the above example, the square of a 5 is nothing but the addition of 5 into 5 for 5 times. We will do the same logic of addition using a programming language. The easiest way of this activity is to use for loop. Let’s understand.

In C++ using for loop we can easily perform this addition as shown below:

``````#include<iostream>

using namespace std;

double getSquareOfNumber(double inputNumber);

int main()
{
double m = 5;

double result = getSquareOfNumber(m);

cout<<"Square of a number using for loop is:"<< result <<endl;

}

double getSquareOfNumber(double inputNumber)
{
double result;

for (int nCnt=0;nCnt<inputNumber;nCnt++)
{
result = result + inputNumber;
}

return result;
}``````

Output:

``Square of a number using for loop is: 25``

## Conclusion:

We hope this article was informative and you are able to calculate the square of a number in C++ using the above methods. If you face any issue then please mention it in the contact form or you can reach out to us using Contact Form.