c语言 用π/4≈1 - 1/3 + 1/5 - 1/7 + ……公式求π的近似值。

如题所述

#include<stdio.h>
int main()
{
    double item=1;
    double sum=0;
    int flag=1, step=1;
    while(item/step>=1e-6 || item/step<=-1e-6)
    {
        sum+=flag*item/step;
        flag*=-1;
        step+=2;
    }
    printf("pi=%lf\n",sum*4);
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答