C++求pi

#include<iomanip>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
int s = 1;
double n = 1, t = 1, pi = 0;
while ((fabs(t))>1e-7)
{
pi = pi + t;
n = n + 2;
s = -s;
t = s / n;
}
pi = pi * 4;
cout << setiosflags(ios::fixed) << setprecision(6) << pi << endl;
system("pause");
return 0;
}

你可能感兴趣的:(C++求pi)