I'm supposed to calculate pi using numbers between 1 through 30 with this formula below
Here is what I've done so far:
int k, outside = 0;
double inside = 0;
cout << "Please enter in a value for k..." << endl;
cin >> k;
while (k < 1 || k > 30)
{
cout << "Invalid --- k must be >=1 and <=30" << endl;
cout << "Please enter in a value for k..." << endl;
cin >> k;
}
outside = pow(2, k);
for (int loopNum = 1; loopNum < k; loopNum++)
{
inside = sqrt(2 + inside);
}
inside = sqrt(2 - inside);
double ApproxPI = outside * inside;
cout << fixed << setprecision(20);
cout << "Approximation of PI = " << ApproxPI << endl;
I feel like I did the formula correctly, but the results aren't accurate. Also, when 'k' is set to 29 or 30, it only gives out the value 0. Does anyone know what I might be doing incorrectly?