#include <iostream>
#include <cmath>
using namespace std;

int main() {

const double PI = 3.141592;
int n, sign = 1;
double sum = 0,test,m;

cout << "This program determines how many iterations of the infinite series   for\n"
        "pi is needed to get with 'n' decimal places of the true value of pi.\n"
        "How many decimal places of accuracy should there be?" << endl;
cin >> n;

double p = PI * pow(10.0, n);
p = static_cast<double>(static_cast<int>(p) / pow(10, n));
int counter = 0;
bool stop = false;

for (double i = 1;!stop;i = i+2) {
    sum = sum + (1.0/ i) * sign;
    sign = -sign;
    counter++;
    test = (4 * sum) * pow(10.0,n);
    test = static_cast<double>(static_cast<int>(test) / pow(10, n));
    cout << "" ;
        if (test == p)
            stop = true;
}
cout << test << '\n';
cout << "The series was iterated " << counter<< " times and reached the value of pi\nwithin "<< n << " decimal places." << endl;
return 0;
}