C language - the square root, Newton iterative method

1022: C language - the square root

Description Title
seek with iterative method. Taking the square root of iterative formula: X [n + 1] = 1/2 (X [n] + a / X [n]) required to obtain the absolute value of the difference obtained before and after the two is less than 0.00001. Output three decimal places

Input
X
output
X of the square root of the
sample input
4
Sample Output
2.000

# include<stdio.h>
# include<math.h> 
int main()
{
	int a;
	double x1,x2=1.0;
	scanf("%d",&a);
	do{
		x1=x2;
		x2=(x1+a/x1)/2;
	}while(fabs(x1-x2)>=0.00001);
	printf("%0.3lf",x2);
	return 0;
}
Published 123 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/Du798566/article/details/104870383