关于#c语言#的问题:(4)编写程序c5-2-10.c,求π的近似值(保留四位小数)

(4)编写程序c5-2-10.c,实现的功能是:按下面公式,求π的近似值(保留四位小数),直到最后一项中的分数小于10-6为止。

img


/*c5-2-10.c*/
#include <stdio.h>
#include <math.h>
int main()
{ 
    int n;
    double pai,a,t;
    n=1;
    a=1;
    t=1+(1.0/(n*(n+2)));
    while(fabs(t)>1.0e-6)
    {
        a=a*t;
        n=n+2;
        t=1+(1.0/(n*(n+2)));
    }
    pai=a*2;
    printf("pai的近似值为%.4lf\n",pai);
    return 0;
}

img


有用的话,请采纳