已知求平方根的迭代公式为xn+1=1/2(xn+a/xn)。
不用while循环,因为while语句中出现的x1没有初始化,所以用dowhile语句
#include<stdio.h>
#include<math.h>
int main() {
int a;
scanf("%d", &a);
double x = 1.0, x1;
do{
x1 = x;
x = (x1 + a / x1) / 2;
} while (fabs(x - x1) > 1e-5);
printf("%0.3lf", x);
return 0;
}