Updated July 4, 2023

## Introduction to Square Root in C++

Today here, let’s learn about one of the well-known mathematical calculations, Square Root. And we will use C++ programming to find the square root of a given number. As already known, C++ is an extension of C programming language with the concept of OOPS being introduced; let’s begin by making our own square root function in C++.

### Logic of Square Root in C ++

To have our square root function, we need to understand the proper logic of how this square root is calculated.

There are many ways to understand the logic too, but we would first start from the basic level.

- We know that the square of a number is a power of 2. In the same way, square root, a number would be the power of ½. We can use a pow function under the h package library for this.

Let’s see how we can represent this in C++.

```
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int num;
float result;
cout<<"Enter number: ";
cin >> num;
result = pow(num,0.5);
cout << "Square root of given number is " << result;
return 0;
}
```

**Output:**

- In another method, we can have logic in a reverse way. Like, the square of the final result should be the number we chose.

Let’s see how we can represent this in C++.

```
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int num;
float result =0 ;
double sq;
cout<<"Enter number: ";
cin >> num;
sq = result*result;
while (sq < num)
{
result = result + 1;
sq = result*result;
if(num == sq)
{
cout<< result;
break;
}
}
cout<< " square root lies between "<< result-1 << " and " << result;
return 0;
}
```

I will not consider the above one perfect, as the output comes properly only if it is a perfect square. This is because; we are increasing the result value by an integer 1 directly. So, if it is not a perfect square, we can show the output below.

We can even write the same logic so that it calculates the exact square root with decimals too. Find it below.

### Finding Root

So, obviously, there are many ways to find a number’s square root. The above two methods can also be used in obtaining the root. Now, let’s see how to write the square root logic code more precisely and logically.

```
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float num,i;
cout<<"Enter number: ";
cin >> num;
for(i=0.01;i*i<=num;i=i+0.01);
if(num==0)
{
cout<<"Square root of given number is 0";
}
else if(num==1)
{
cout<<"Square root of given number is 1";
}
else if( num < 0 )
{
cout<<"Enter a positive number to find square root";
}
else
{
std::cout << std::fixed;
std::cout << std::setprecision(3);
cout<<"Square root of given number is " <<i;
}
}
```

Yes, the code seems short and simple. Here goes the logic:

- We are declaring our two values, a number which is taken as input and one is our result.
- We ask the user to input a number for which we need to write the square root.
- For loop, we will initiate the i value to 0.01 as we need our results to be in decimal points.
- Then, we will execute that for a loop until the square of the i value is less than the user-inputted value.
- And we will increase the i value with 0.01 only, as we need decimal points, and we have to increase the i value proportionally as per the declaration.
- If observed, we have kept a semicolon at the end of for loop, which makes the loop run without executing any inner statements until the condition is satisfied.
- Now, we can make if a condition for the inputted value is zero and then return 0 instantly.
- In the same way, give the output as 1 if the inputted value is one.
- In the next else if condition, we gave a condition of any negative value that is provided as user input.
- On the else condition, we are going to output i value.
- We ensured consistency using a method that set the precision and fixed the number of decimal places to three digits.

Note: The declaration of the **iomanip** package and including it in the program is mandatory for this set precision method.

The output is attached below:

This way, we can easily calculate the square root of a number perfectly. As an exercise, can you try finding the square root of a number in any other way?

### Conclusion

So, in this way, we can have our square root function in C++. We can even find square roots using Euclidian, Bayesian, and sorting techniques. And as everyone is aware, we can even directly calculate the square root using the sqrt function.

### Recommended Articles

This is a guide to the Square Root in C ++. Here we discuss the introduction and logic of square root in C ++ and root finding. You may also look at the following articles to learn more –