您的位置:首页 > 其它

利用公式计算π=4(1-1/3+1/5-1/7+1/9-...)的近似值,直到括号中最后一项的绝对值小于0.000001为止

2012-09-15 18:24 1436 查看
#include<iostream>

#include<cmath>

using namespace std;

int main( )

{int a=1;

 double b=1,c=1,pi=0;

 while(fabs(c)>=0.000001)

 {pi+=c;

 b+=2;

 a=-a;

 c=a/b;

 }

 pi=4*pi;

 cout<<"pi="<<pi<<endl;

 return 0;

}

 

运行结果:

 

pi=3.14159

Press any key to continue
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c
相关文章推荐