How to make a square in C++
Determine a number's square for an integer x. To obtain the square number of the given number, we must multiply it by itself. Any number raised to the power of two is always used to represent the square term. As an illustration, the square of 7 is equal to 7 times 7, or 7×7 = 49.
Therefore, we can just multiply single-digit numbers by themselves to determine their square.
Examples :
Input: x = 6
Output: 36
Input: x = 9
Output: 81
Input: x = 13
Output: 169
Method 1:
Adding x to the result repeatedly is a simple solution.
Here's how this concept was put into practice.
#include <iostream>
using namespace std;
int square(int x)
{
if (x < 0)
x = -x;
int value = x;
for (int i = 1; i < x; i++)
value += x;
return value;
}
// Driver code
int main()
{
for (int x = 1; x <= 9; x++)
cout << "x = " << x << ", x ^ 2 = " << square(x)
<< endl;
return 0;
}
Output
Method 2:We can use bitwise operators to do it in O(Logn) time. This fact forms the basis of the concept.
If x == 0 then square(x) = 0; otherwise, if x is even then square(x) = 4 * square(x/2). When x is odd, square(x) equals 4*square(floor(x/2)) + 4*floor(x/2) + 1.
For instance,
square(8) = 4*square(4)
square(5) = 4*square(2) + 4*2 + 1 = 25;
square(9) = 4*(square(4)) + 4*4 + 1 = 81.
How does one go about doing this?
The formula for an even number, x = 2*n, can be expressed as x2 = (2*n)2 is equal to 4*x2.
The formula for an odd number, n = 2*x + 1, can be expressed as n2 = (2*x + 1)2 is equal to 4*x2 + 4*x + 1.
One can compute floor(x/2) by applying a bitwise right shift operator. Two*x and four*x can be computed.
The execution of the concept above is shown below.
#include <bits/stdc++.h>
using namespace std;
int square(int x)
{
if (x == 0)
return 0;
if (x < 0)
x = -x;
int n = x >> 1;
if (x & 1)
return ((square(n) << 2) + (n << 2) + 1);
else
return (square(n) << 2);
}
// Driver Code
int main()
{
// Function calls
for (int x = 1; x <= 9; x++)
cout << "x = " << x << ", x ^ 2 = " << square(x)
<< endl;
return 0;
}
Output:
Method 3:We may find the square of a given number {number} by multiplying it by {number * number}.
Now, express one of {number} to the power of {2} in square {number * number}. Look at the examples below.
For instance, if number = 9, square(number) = 9 * 9 = 9 * (6 + 3) = (9 * 6) + (9 * 3) = 81
If number is equal to 12, then square(number) would be 12 * 12 = 12 * (8 + 4) = (12 * 8) + (12 * 4) = 144
The left shift bitwise operator can be used to do multiplication to the power of two.
The execution of the concept above is shown below.
#include <iostream>
using namespace std;
int square(int number)
{
if (number < 0) number = -number;
int value = 0, t = number;
while (t > 0)
{
int ps = 0, ct = 1;
while ((ct << 1) <= t)
{
ct = ct << 1;
++ps;
}
value = value + (number << ps);
t = t - ct;
}
return value;
}
int main()
{
// Function calls
for (int x = 10; x <= 20; ++x)
cout << "x = " << x << ", x ^ 2 = " << square(x) << endl;
return 0;
}
Output:
Method 4:#include <iostream>
using namespace std;
int square(int x)
{
if (x < 0)
x = -x;
int pow = 0, value = 0;
int n = x;
while (n) {
if (n & 1) {
value += (x << pow);
}
pow++;
n = n >> 1;
}
return value;
}
// Driver code
int main()
{
// Function calls
for (int x = 10; x <= 20; ++x)
cout << "x = " << x << ", x ^ 2 = " << square(x)
<< endl;
return 0;
}
Output:
← Prev Next →