1/* This program calculates the first n approximations of the number pi
2from the infinite series
3pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
4All the approximations and displayed
5*/
6
7#include <iostream>
8#include <iomanip>
9
10using namespace std;
11
12int main() {
13
14 int n{ 170000 }, index=-1;
15 double approximation{ 1 };
16
17 cout << "approximation 0: 4" << endl;
18
19
20 for (int i = 1; i <= n; i++) {
21
22 approximation += pow(-1, i) / static_cast<double>(2 * i + 1);
23 cout << "approximation " << i << ": ";
24 cout << setprecision(9) << setw(10) << left << fixed; // do not remove fixed to see all 9 decimal places
25 cout << 4 * approximation << endl;
26
27 if (static_cast<int>(4 * approximation * 100000) == 314159 and index == -1) {
28 cout << "The " << i << "th approximation begins with 3.14159\n";
29 index = i;
30
31 char k;
32 cout << "Press Enter to continue";
33 cin >> k;
34 }
35 }
36
37 cout << "The " << index << "th approximation begins with 3.14159\n";
38 return 0;
39}