帮我用C语言写一个不调用math.h的开方代码
时间: 2023-03-30 13:00:26 浏览: 136
好的,以下是一个不调用math.h的开方代码:
```c
#include <stdio.h>
double sqrt(double x) {
double guess = 1.;
double diff = 1.;
while (diff > .0001) {
double new_guess = (guess + x / guess) / 2.;
diff = new_guess - guess;
if (diff < ) {
diff = -diff;
}
guess = new_guess;
}
return guess;
}
int main() {
double x;
printf("请输入一个数:");
scanf("%lf", &x);
printf("该数的平方根为:%lf\n", sqrt(x));
return ;
}
```
这个代码使用了牛顿迭代法来计算平方根,不需要调用math.h库中的sqrt函数。
阅读全文