#include<iostream> #include<cstdio> #include<math.h> int main() { double x; double x1 = 1.0, x2; scanf("%lf", &x); x2 = 0.5 * (x1 + x / x1); while(fabs(x1-x2) >= 0.00001) { x1 = x2; x2 = 0.5 * (x1 + x / x1); } printf("%.3lf", x2); return 0; } |
Double click to view unformatted code.