(C语言)用迭代法求x=根号a,要求前后两次求出来的x的差的绝对值小于10^-5

用迭代法求x=根号a,要求前后两次求出来的x的差的绝对值小于10^-5

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main() {
	double a, x0, x1;
	printf("请输入a的值\n");
	scanf("%lf", &a);
	x0 = a / 2;
	x1 = (x0 + a / x0) / 2;
	while (fabs(x0 - x1) >= 1e-5) {
		x0 = x1;
		x1 = (x0 + a / x0) / 2;
	}
	printf("%f的算术平方根为:%f\n",a, x1);
	system("pause");
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41071068/article/details/89425801