while loop - Square root program in C without using sqrt function -


I create a program in C to find the approximate square root of Ng = 0.5 (using LG) in the formula I am trying. + N / lg). I have so far:

  #include & lt; Stdio.h & gt; # Include & lt; Math.h> Int main () {double n; Double LG = 1; Double ng; Printf ("Enter Number"); Scanf_s ("% Left", & amp; n); Do {ng = (.5 * (LG + N / LG)); LG = NG; } While ((ng * ng-n) & lt; .005); Printf ("root is% lf", ng); }  

This structure works fine in Java, but does not implement in loop C.

Thanks for any advice.

You do not want to loop, while NG * NG-n . Less than 005 You want a loop, while NG * NG is more than desired n . Distance between NG * NG and n is fibers (ng * ng - n) .


Comments