编程求教2

来源:百度知道 编辑:UC知道 时间:2024/05/29 02:57:12
用∏/4≈1-1/3+1/5-1/7+1/9-…公式求∏的近似值,直到最后一项的绝对值小于1/1000000。
这该如何编写啊?请高手指教

#include "stdio.h"
#include "math.h"
main()
{int i;
float f=0.0,s=0.0,∏;
for(i=0;i<1000000||fabs(f)<1/1000000;i++)
(if(i%2==0)
f=1.0/(2*i+1);
else
f=(-1)*1.0/(2*i+1);
s+=f;
)
∏=s*4;
printf("∏得值大约等于%6.2f\n",∏);
}