计算∏的近似值 C语言

#include <stdio.h>

int main()
{
double a,b,c;
a=1;b=3;c=1/b;
while(1/b>=10e-6)
{
a=a-c;
b=b+2;
c=c*(-1);
}
printf("∏的值为%f",a*4);
}
但是算出的值却为2.666667,哪里出错了呢?

/*

π的近似值为:3.141573

Press any key to continue

*/

// 莱布尼茨级数π/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...... +
#include <stdio.h>
#include <math.h>

int main() {
double n = 1,sign = 1,pai = 0;
double item = sign/(2 * n - 1); // 项
while(fabs(item) >= 10e-6) {
pai += item;
sign = -sign;
++n;
item = sign/(2 * n - 1);
    }
    printf("π的近似值为:%lf\n",pai * 4);    
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-11-25
#include <stdio.h>
#include <math.h>
int main()
{
int f;
float n;
double t,pi;
t=1;pi=t;f=1;n=1.0;
while(fabs(t)>=1e-6)
{
n=n+2;
f=-f;
t=f/n;
pi=pi+t;
}
pi=pi*4;
printf("pi=%10.6f\n",pi);
return 0;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网