c程序设计第四版谭浩强
用迭代法求 x=根号a,求平方根的迭代公式为 Xn+1=(1/2)(Xn+a/Xn)
要求前后两次求出的X的差的绝对值小于10-5。
以下是此题的【c源代码】,需要【c++源代码】请点击进入
#include <stdio.h>
#include <math.h>
int main()
{
float a,x0,x1;
printf("enter a positive number:");
scanf("%f",&a);
x0=a/2;
x1=(x0+a/x0)/2;
do
{x0=x1;
x1=(x0+a/x0)/2;
}while(fabs(x0-x1)>=1e-5);
printf("The square root of %5.2f is %8.5f\n",a,x1);
return 0;
}
本文链接:http://www.wb98.com/c/post/tanhaoqiang_5.13.html
本站文章搜索: