10、求圆周率

#include<iostream>
#include<math.h>
using namespace std;
int main(){
   
   float n=1.0;  //表示的是分母 
   
   float t=1.0,ans=0;  //t为循环需要加的项   ans=表示的是整个表达式的值 
   
   int  s=1;
                     
   while(fabs(t)>1e-6){
       ans=ans+t; //进行累加操作
       n=n+2; //更改分母的值。
       s=-s;  //更改正负号 
       t=s/n;  //  更改t 的值 
       
   }
    
   cout<<ans*4<<endl;
    system("pause");
}

π/4=1-1/3+1/5-1/7+1/9+.....;

我们可以求出 π的值。

猜你喜欢

转载自blog.csdn.net/qq_30272539/article/details/81200459