title: Finding square root without using sqrt function?
body: <p>I was finding out the algorithm for finding out the square root without using sqrt function and then tried to put into programming. I end up with this working code in C++</p>
  
  <pre><code>    #include &lt;iostream&gt;
      using namespace std;
  
      double SqrtNumber(double num)
      {
               double lower_bound=0; 
               double upper_bound=num;
               double temp=0;                    /* ek edited this line */
  
               int nCount = 50;
  
          while(nCount != 0)
          {
                 temp=(lower_bound+upper_bound)/2;
                 if(temp*temp==num) 
                 {
                         return temp;
                 }
                 else if(temp*temp &gt; num)
  
                 {
                         upper_bound = temp;
                 }
                 else
                 {
                         lower_bound = temp;
                 }
          nCount--;
       }
          return temp;
       }
  
       int main()
       {
       double num;
       cout&lt;&lt;"Enter the number\n";
       cin&gt;&gt;num;
  
       if(num &lt; 0)
       {
       cout&lt;&lt;"Error: Negative number!";
       return 0;
       }
  
       cout&lt;&lt;"Square roots are: +"&lt;&lt;sqrtnum(num) and &lt;&lt;" and -"&lt;&lt;sqrtnum(num);
       return 0;
       } 
  </code></pre>
  
  <p>Now the problem is initializing the number of iterations nCount in the declaratione ( here it is 50). For example to find out square root of 36 it takes 22 iterations, so no problem whereas finding the square root of 15625 takes more than 50 iterations, So it would return the value of temp after 50 iterations. Please give a solution for this.</p>