Change your code, replacing everything nwith iin for loop:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long double n;
cin >> n;
long double first_part = 0.0, second_part = 0.0, pi = 0.0;
for(int i = 0; i <= n; i++)
{
first_part += (pow(-1, i)) / ((2 * i + 1) * pow(5, 2 * i + 1));
second_part += (pow(-1, i)) / ((2 * i + 1) * pow(239, 2 * i + 1));
}
pi = (first_part * 16) - (second_part * 4);
cout << pi << endl;
return 0;
}
I ran the above code and found outout:
3.14159
Caution: pow(239, 2 * n + 1)) May Overflow second_part! Since it doublehas a range:
1.7E +/- 308 (15 digits)
Link here .
source
share