C++/c++

Advertisement


Question
hello!!

what is the code in c++ for finding square root of a number without using the function sqrt() in math.h???

compiler i use is visual basic 6.0.....  

Answer
Edit:
When I was a student, I learned the Newton's method for finding the roots of an function. The method, along with an example for square roots is in http://en.wikipedia.org/wiki/Newton%27s_method
I would recommend that method.






There are many ways of finding the square root of a number. Try reading the start of
http://en.wikipedia.org/wiki/Square_root
until you reach the Taylor series.
The Taylor series might be interesting to implement. Can you find the pattern?

Also the article
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
has many techniques including some C source code. Find one you think you can implement and try it.

If you just want an answer, try the following that I found at

http://www.dreamincode.net/forums/showtopic109394.htm

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

using namespace std;


double sqroot(float m)
{
    double i=0;
  double x1,x2;
  while( (i*i) <= m )
         i+=0.1;
  x1=i;
  for(int j=0;j<10;j++)
  {
       x2=m;
     x2/=x1;
     x2+=x1;
     x2/=2;
     x1=x2;
  }
  return x2;
}

int main()
{
    cout<<"Square Root using sqroot()= "<<sqroot(2)<<endl;

  return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.