#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double pi = 1;
long i;
long n;
cout << "Enter the value of n: ";
cin >> n;
cout << endl;
for (i = 1; i < n; i++)
{
if (i % 2 == 0)
pi = pi + (1.0 / (2.0 * i + 1.0));
else
pi = pi - (1.0 / (2.0 * i + 1.0));
}
pi = 4 * pi;
cout << endl << "pi = " << pi << endl;
return 0;
}
Iterative approximation algorithms are expected to be relatively inaccurate when they are only allowed to iterate a small number of times. I don't know what kind of answer you're expecting. "Use a different algorithm"?
I know what you mean. It could be that I misunderstood the goal of this assignment (it's for school). "Use a different algorithm" is probably good advice, since understanding the problem is the first step to good programming. I suppose. Thank you.