Monday, June 28, 2010

C code for finding square root of a number without using sqrt() function.

 The code has the following features
  •     This following code finds a square root for a given number.
  •     You can also find square root for a floating number.
  •     This code is using a Mathematical formula.
Here is the code

  float squareRoot(float n)
  {  float prev=0,cur=1;
     while(prev!=cur)
      {   prev=cur;
          cur=0.5*(prev+(n/prev));
       }
     return cur;
  }                         //Try this code..It works great..

5 comments:

Sourabh Singh said...

Hats off to u..!

Anonymous said...

Awesome.. :)

Anonymous said...

Great job

Unknown said...

its advisable not to compare floating point numbers since there may be imprecision in the representation of fps. And it precisely the reason why most compilers / functions dont use the above method. Not that those developers didn't know. :P

Test said...

grt