码迷,mamicode.com
首页 > 其他好文 > 详细

求π的近似值

时间:2018-11-19 17:32:29      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:turn   int   次数   1.0   include   print   nbsp   style   sig   

#include <stdio.h>
#include <math.h>
int main()
{
    int sign,n;
    float p,i,t;
    i=1.0;
    sign=1;
    n=0;
    p=0.0;
    t=1.0;
    while(fabs(t)>=1e-6)
    {            
        sign=-sign;    
        i=i+2;
        p=p+t;
        t=sign/i;
        n=n+1;
    }
    p=p*4;
    printf("%f\n",p);//π的近似值
    printf("%d\n",n);//执行次数
    return 0;
}

 

求π的近似值

标签:turn   int   次数   1.0   include   print   nbsp   style   sig   

原文地址:https://www.cnblogs.com/qfdy123/p/9983732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!