一个c语言程序执行的问题?

来源:百度知道 编辑:UC知道 时间:2024/05/24 05:37:08
#include <stdio.h>
#include <conio.h>
#include <math.h>
double fun(double eps)
{int i=1;double t,s=1.0;
t=1.0;
while(t>=eps)
{
t=t*i/(2*i+1);
i++;
s+=t;
}
return (2*s);
}
main()
{
double x,n;
printf("Input eps: ");
scanf("%1f",&x);
n=fun(x);
printf("\neps=%1f,PI=%1f\n",x,n);
}
函数fun(),它的功能是:根据以下公式求π的值(要求满足精度0.005,即某项小于0.005时停止迭代):
π/2=1+1/3+1*2/(3*5)+1*2*3/(3*5*7)+1*2*3*4/(3*5*
7*9)+…+1*2*3*…*n/(3*5*7*…*(2n+1))
程序运行后,如果输入精度0.0005,则程序输出3.141593。
帮忙看看啊:为什么输入了数据后运行不出结果呢??????问题在哪呢????????

我怀疑你把所有的输入输出参数都搞错了吧
应该是%lf(L的小写),你写成了%1f(1、2、3、4的1).

有结果啊。
Input eps: :0.0005
0.0005 3.14111

受教

while(t>=eps)
{
t=t*i/(2*i+1);
i++;
s+=t;
}
while()语句中的算法有错。比如,当i=4时,t=1*2*3*4/(3*5*7*9)吗?