## Introduction to Square Root in C++

Today here, let’s learn about one of the well-known mathematical calculations, Square Root. And we are going to use C++ programming in finding 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 in making our own square root function in C++.

### Logic of Square Root in C ++

For having our square root function, we need to understand the proper logic of how actually this square root is being calculated.

There are actually 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 ½. For this, we can use a pow function under the h package library.

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 obtained should be the number which 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 as below.

4.5 (3,249 ratings)

View Course

We can even write the same logic in such a way that it calculates the exact square root with decimals too. Find it below.

### Finding Root

So, obviously there are many ways in finding the square root of a number. The above two methods can also be used in obtaining the root. Now, lets’ see how we can 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, 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.
- Asking the user to input a number for which we need to write the square root.
- In for loop, we are going to initiate i value to 0.01 as we need our results to be in decimal points.
- Then, we are going to execute that for a loop until the square of i value would be less than the user inputted value.
- And we are going to increase i value with 0.01 only, as we need decimal points and we have to increase 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 given as user input.
- On the else condition we are going to output i value.
- Here, we have used a set precision method and fixed the number of decimal places till 3 digits, so that the output we obtain will be uniformly obtained.

Note: The declaration of **iomanip** package and including in the program is mandatory to use 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 through any other way?

### Conclusion

So, in this way, we can have our own square root function in C++. We can even find square root using Euclidian, Bayesian and even through sorting techniques. And as everyone in anyways is aware of, we can even directly calculate the square root using 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 ++ along with root finding. You may also look at the following articles to learn more –