C++: Program to Find the Square Root of a Number Without using Inbuilt sqrt() Function

Hey folks, hope you people are doing well.

Today I am going to share a program of finding the square-root of a number without using any inbuilt library functions.

So, the basic algorithm goes like this,

Suppose that you want to find square root of the number 49. You know that the square root lies between 0 to 49. So how do you check it.

Let say you take the lower bound as 0 and upper bound as 49. now you take the exact middle value and check whether the square of that middle value is anywhere near to the number.


temp = (lower_bound + upper_bound)/2


You will get temp = 24.5. and temp * temp = 600.25
That way to larger than the number, so we now know that the square root lies between 0 to 24.5. We will continue this procedure till we find the exact square root of the given number.

Hope you got the algorithm. Now the program. Here is the program I've created to test the algorithm, and guess what, it works pretty well..


#include <iostream>
using namespace std;

double sqrtnum(double num) //Our main function to find the square root.
{
double lb=0; //lower bound initialization
double ub=num, temp=0;

/*upper bound initialization and taking 
a temporary variable to save partial results.*/
 

                 int itr = 40;
/*We take iteration variable to make sure 

we perform the loop enough time so that 
we get closer to the exact value of the square root. 
Remember the more the iteration, 
the closer you are to the answer.*/

while(itr != 0)
{
temp=(lb+ub)/2;
if(temp*temp==num) 

                  //Checking whether we reached an exact number.
{
return temp;
}
else if(temp*temp > num)
                   /*If not, re- initialize the 

                    upper or lower bound according to the conditions.*/
{
ub=temp;
}
else
{
lb=temp;
}
itr--;
}
return temp;
}

int main()
{
double num, result;
cout<<"Enter the number\n";
cin>>num;

if(num < 0)
{
cout<<"Can not process the squareroot of negetive number\n";
return 0;
}

cout<<"The square root of the number is: "<<sqrtnum(num);
return 0;
}



Points to remember:
1. The more you keep the value of the iteration in the function, the closer you will get to the answer.
2. Taking all the variables as double is to make sure we can handle larger numbers and the floating points.

Please analyse and let me know what do you think about it. If there is any improvement required please leave a comment below so that I can also learn more.

..... Later folks... :)

8 comments: Leave Your Comments

  1. Good one... Thnx for sharing mate.... :)

    ReplyDelete
  2. Excillent dude .. Good explantion .. keep it up ..

    ReplyDelete
    Replies
    1. Thank you... Please stay connected for more articles with same quality. You can suggest some, too... :)

      Delete
  3. Nice explanation, just avoid comparing float numbers with "==". You can google and learn more about the issues. Here is slight improved version:

    double my_sqrt(double value)
    {
    double result = 0;

    double start = 0;
    double end = value;
    double EPSILON = std::numeric_limits::epsilon();// 0.001;

    while (true)
    {
    result = (start + end) / 2;
    if (abs(result*result - value) < EPSILON)
    {
    return result;
    }
    else if (result*result > value)
    {
    end = result;
    }
    else
    {
    start = result;
    }
    }
    return result;
    }

    ReplyDelete
    Replies
    1. Thank you for the suggestions.. I will surely keep this in mind.. Please keep on sharing your valuable knowledge here to make this place better than it is... Thank you for your contribution...

      Delete
  4. Your solution works good for most cases but it's not complete. For positive numbers less than 1, the square root is greater than the actual number. But by setting the lower bound at 0 and the upper bound at the number, you will never find square roots of numbers that are less than one. The change you should make is that if the number is less than 1, the upper bound should be set to 1 and not the number itself.

    ReplyDelete
  5. C++ program to find Square Root of a Number

    Thanks for sharing this code. It's really helpful. Keep sharing.

    ReplyDelete
  6. Very good algorithm. But That works only with perfect square root, e.g. if I want to calculate the square root of number 3, It seems that will not work. Please correct me if I am wrong

    ReplyDelete

+