fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double sqrt2(double n) {
  5.  
  6. float x = n,
  7. y = 1.0,
  8. e = 0.0000000001;
  9. while(x-y>e) {
  10. x = (x + y ) / 2;
  11. y = n / x;
  12. }
  13. return x;
  14.  
  15. }
  16. int main(void) {
  17. printf("%.7f\n", sqrt2(2));
  18. printf("%.7f", sqrt(2));
  19. return 0;
  20. }
  21.  
Success #stdin #stdout 0.01s 5468KB
stdin
Standard input is empty
stdout
1.4142135
1.4142136