给定程序中函数fun的功能是:用递归算法求形参a的平方根。求平方根的迭代公式如下:

 

X1=1/2(x0+a/x0)

例如,a为2时,平方根值:1.414214

  

#include <stdio.h>
#include <math.h>
double fun(double a, dounle x0)
{ double x1, y;
x1=(x0+ a/x0)/2.0;
if( fabs(x1-x0)>=0.00001 )
y=fun(a,x1);
else y=x1;
return y;
}
main( )
{ double x;
printf("Enter x: "); scanf("%lf",&x);
printf("The square root of %lf is %lf\n",x,fun(x,1.0));
}

posted @ 2017-11-02 22:53  can丶  阅读(1812)  评论(0编辑  收藏  举报