Im wondering how do I get the square root of a number in C. Can any body help. Thanx

Printable View

- 05-15-2002Unregisteredsquare root function
Im wondering how do I get the square root of a number in C. Can any body help. Thanx

- 05-15-2002Prelude
>Im wondering how do I get the square root of a number in C.

The easiest way would be the sqrt() function in math.h.

-Prelude - 05-16-2002Lynux-Penguin
Example:

Code:`#include <stdio.h>`

#include <math.h>

int main()

{

printf(sqrt(20));

return 0;

}

- 05-16-2002Salem
> printf(sqrt(20));

First parameter to printf is a string

printf( "%f\n", sqrt(20)); - 05-19-2002stautze
You could also easily write your own by using Newton's method of successive approximations.

- 05-19-2002Unregistered
or use a very simple ( time consuming ) method:

use a loop using long doubles and do a comparison of number * number to equal the desired number. a very basic approach. this will have some trouble with some numbers, though. didn't test it yet.

- toaster - 05-19-2002stautze
>use a loop using long doubles and do a comparison of number * number to equal the desired number. a very basic approach.

While your at it, you could reinvent the wheel. - 05-19-2002Unregistered
haha. no.

- 05-19-2002Shiro
This is my favourite math reference:

http://mathworld.wolfram.com/SquareRootAlgorithms.html