C语言计算π(pi)值

作者: 李药师_hablee | 来源:发表于2019-03-31 14:43 被阅读0次

    题目

    QQ图片20190331144301.png

    代码

    #include<stdio.h>
    #include<stdlib.h>
    //1+1/3+1/3*2/5+1/3*2/5*3/7...
    
    // cnt/divident
    //cnt++; divident += 2;
    int main()
    {
        int divident = 1;
        int cnt = 0;
        double t1=1,t2=1;
        double evenPi = 1;
        double evenPiPre = 0;
        double precision;
        printf("输入精度:");
        scanf_s("%lf", &precision);
    
        while (((2*evenPi) - (2*evenPiPre)) > precision)
        {
            evenPiPre = evenPi;
            t1 = t1*t2;
            cnt++;
            divident += 2;
            t2 = (double)cnt / divident;
            evenPi = evenPiPre + t1*t2;
            printf("%f=%f+%f*%f\n", evenPi, evenPiPre, t1,t2);
        }
        printf("PI = %7f\n", evenPi*2);
        system("pause");
        return 0;
    }
    

    结果

    输出.PNG

    相关文章

      网友评论

        本文标题:C语言计算π(pi)值

        本文链接:https://www.haomeiwen.com/subject/rozybqtx.html