When we square a number, we simply multiply it by itself. We have to utilize a header file if we want to get a square of a number. Header files allow us to declare a function with a type placeholder that the compiler will fill in at compile-time based on how the function is used.
In C++, when we need a square of any given number, numerous methods are available. Let’s talk about a few of them:
Find square of a number using Power function
Using the Power function, we may square any value. For it, we will have to include <cmath> library. We must pass the Base value to be squared and the Power value into the function. In C++, the power() function works as a square operator in this instance.
#include <cmath>
using namespace std;
int main(){
int b = 34;
int p = 2;
float result = pow(b,p);
cout<< "Square = "<< result <<endl;
}
The “cmath” library has a predefined function called pow. Therefore, we must integrate this library at the beginning of the code. We declare two variables in the body of the main function. The first variable is defined to store the value of the base. The value here is “34”. The second variable is declared to store a power value that is 2 in this program. These two variables have an integer data type.
Furthermore, we apply the pow() function. We pass two arguments (base value and power value) for this function. It returns the result. The output is stored in a new variable termed ‘result’.
Find square of a number using for loop
If we need to get a square of a value without applying multiplication or division, we must use another logic to get the given value’s square. In the succeeding program, we utilize for loop.
using namespace std;
float Sqr(float number){
float a=0.0;
for(int j=0;j<number;j++){
a = a + number;
}
return a;
}
int main(){
cout<< "Square = " << Sqr(20.0) <<endl;
}
We declare the function sqr(), and its data type is ‘float’. We pass a floating-point number as an argument to this function. Moreover, we utilize a for loop in this instance to add a number. First, we allocate ‘0’ to the variable ‘j’ in the initialization segment. The test condition checks the value of that variable. ‘j<number’ tests the loop every time to see if ‘j’ is less than the given value. ‘j++’ increases the variable ‘j’ every time the loop is implemented.
Generally, any indication can be utilized to increment the loop variable. Once the loop ends, the variable is still defined and holds the value allocated by the latest increment. In the code, we add 20 + 20….Up to 20 times. Therefore, after the addition, 20 square (400) is created. Compiling and running the above program produces this type of output:
Find square of a number using while loop
If we use a while loop to find the square of any number, we will need to include an odd number so that the square is created at the end of the program.
using namespace std;
float Square(float value)
{
float OddNum = 1.0;
float SquareNum = 0.0;
value = abs(value);
while (value--)
{
SquareNum = SquareNum + OddNum;
OddNum = OddNum + 2;
}
return SquareNum;
}
int main()
{
cout<< "Square of Number = " << Square(5.0) <<endl;
return 0;
}
In this instance, after integrating the library ‘#include <iostream>, we define the ‘square’ function. The floating-point value is passed as an argument to this function. Further, we declare variables ‘OddNum’ and ‘SquareNum’ and assign them values. Afterward, we apply the absolute function ‘abs()’ that converts the negative value to the positive when we enter any negative value. We use a while loop.
The compiler first evaluates the test condition when a while statement is implemented. Once the body of the loop is implemented, the condition is assessed again, and if it becomes true, the body of the loop is implemented once again. This procedure continues till the test condition becomes false. Once it is false, the control is passed on to the first statement after the end of the body of a loop. In every evaluation, ‘2’ is added to the value ‘OddNum’ to make it odd.
When the above code is executed, it will give the output shown below:
Conclusion
In this article, we have deliberated three techniques for finding the square of the number in C++. First, we see how we get the square of a number by using the pow() function. Likewise, we utilize the ‘for’ loop and ‘while’ loop for finding the square. By using for loop, we perform the addition of any number. Similarly, we add an odd number by using the while loop to get the square.