My equation output is incorrect for this homework problem
The value for pi can be determined by the series equation
Write an interactive program that asks the user how many terms of the series equation to use in approximating
. Then calculate and display the approximation. Test your program for 10, 20 and 30.
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double series, pi;
int n, i;
cout <<"Enter the number of terms: \n";
cin >>i;
for (n=1; n<=i; n++)
{
series=((-1)^(n-1))/(2*n-1);
pi=series+pi;
cout<<n<<endl;
cout<<pi<<endl;
}
cout <<"The pi in "<<i<<" terms is "<<pi*4 <<endl;
system ("pause");
return 0;
}
My output is wrong I suspect it is from the equation 'series'
Thank you.