Calculate the value of pi from the infinite series
pi = 4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+...
------------------------------------------------------------------
#include <iostream>
using namespace std;
double pi;
int main()
{
int num;
cout << "Enter integer:";
cin >> num;
for (int x=1; x<=num; x++)
{
if(x==1){
pi =4;
cout << x <<"->"<< pi << endl;
}
else if(x%2==0)
{
pi = pi - (4.0/(2.0*x-1.0));
cout << x<< "->" << pi << endl;
}
else{
pi = pi+(4.0/(2.0*x-1.0));
cout << x <<"->" << pi << endl;
}
}
system("pause");
}
- Details
- Written by Alece
- Published: 23 October 2013