I have only just begun programming and i am a bit stuck on a current logic problem. The program i made (which is very simple) for the purpose of calculating pi is based on the infinite alternating series:

4/1 - 4/3 + 4/5 - 4/7 …

I've already worked out the mathematics and it works, but it's very inefficient (since the input is the number of iterations). Obviously the series converges to pi as it goes towards infinity, but as there is no way to repeat a program an infinite number of times (without waiting an infinite amount of time) the technique i've employed is flawed. I was wondering if someone could give me a suggestion as to how i could make my program more efficient and get a more accurate reading of pi (without waiting a really long time for the answer).

Thank you.

The source code i have up until now is:

Code:#include <iostream> #include <cmath> using namespace std; int main() { double pi, series, entry, number = 1, alternating_sign; int count; char ans; do { cout << "Entry:"; cin >> entry; for (count = 0; count < entry; count ++) { alternating_sign = pow((-1.0), (count)); series += alternating_sign / (number); number+=2; } cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(10); pi = 4*series; cout << "Pi equals " << pi << endl; cout << "Try again?\n"; cin >> ans; } while (ans == 'Y' || ans == 'y'); return 0; }