C语言求圆周率

2024-11-20 07:13:12
推荐回答(1个)
回答(1):

#include 

int main()
{
    float f;
    double pi,i,sign;
    while(scanf("%f",&f)==1)
    {
        pi=0;
        i=1;
        sign=1;
        do
        {
            pi+=sign*1.0/i;
        }while(1.0/i>=f&&(sign=-sign)&&(i+=2));
        printf("%lf\n",pi*4.0);
    }
    return 0;
}