【C语言】用公式求π的近似值,要求最后一项的绝对值不小于10的-6次方

版权声明:原创文章最终版权归作者所有,作者保留该文章最终解释权!未经作者允许不可用于个人及商业用途! https://blog.csdn.net/u011182346/article/details/86492444

手机用户:

 

代码: 


#include <stdio.h>
#include <math.h>
int main() {

    float s = 1,pi = 0,i = 1.0,n=1.0;//s 是项数,pi是π,i是当前项的值,n是分母
    while(fabs(i) >= 1e-6){//最后一项的绝对值不小于1e-6

        pi += i;
        n = n+2;
        s = -s;
        i = s/n;

    }
    pi = 4 * pi;

    printf("PI Value:%.6f\n",pi);
    return 0;
}

运行结果: 

PI Value:3.141594

猜你喜欢

转载自blog.csdn.net/u011182346/article/details/86492444