program to find the square root of a number without using sqrt is :::



#include <iostream.h>
#include <math.h>

int main()
{
  const float tol = 0.000005; // relative error tolerance
  float value;
  float oldval, newval;
  cout << "Square root of a number"
       << endl << endl;
  cout << "Enter a positive number: ";
  cin >> value;
  if (value < 0.0)
    cout << "Cannot find square root of negative number"
         << endl;
  else
     if (value == 0.0)
        cout << "square root of "
             << value
             << " is 0.0"
             << endl;
     else
       {
         oldval = value; // take value as first approximation
         newval = (oldval + value/oldval)/2;
         while (fabs((newval-oldval)/newval) > tol)
           {
             oldval = newval;
             newval = (oldval + value/oldval)/2;
           }
         cout << "square root of "
              << value
              << " is " << newval
              << endl;
       }
 return 0;
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to