fun函数设置循环,当x0-x1的绝对值小于0.000001循环结束。
#include
<stdio.h>
#include
<math.h>
float
fun()
{float
x,n=0.0,root
while(root>=0.000001||root<=-0.000001)
{
x=n
n=cos(x)
root=x-n
}
root=n
return
root
}
void
main()
{
float
f=fun()
printf("root=%f\n",f)
}
while 和do while是不同地第二个改成
#include <math.h>
#include <stdio.h>
main()
{float x,t,c
int a
scanf("%d",&a)
x=1
t=xx=(1.0/2)*(x+a/x)c=x-t
while(fabs(c)/x>1e-5)
{
t=xx=(1.0/2)*(x+a/x)c=x-t
}
printf("%f",x)
getch()
}
才会等价于第一个程序