Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

solve in c++ program please Exercise #3: Calculate the Value of Pi Write a C++ p

ID: 3891752 • Letter: S

Question

solve in c++ program please
Exercise #3: Calculate the Value of Pi Write a C++ program that calculates an estimate of the value of Pi In mathematics, Pi is approximated by the following formula: Pi 4 4/3 4/5 - 4/7 + 4/9-4/11 +4/13 - 4/15 4/17... The more terms you use, the closer to Pi you get (The above equation shows 9 terms). For example, if you use 5 terms to calculate Pi, your formula would be as follows Pi= 4-4/3 + 4/5-4/7 + 4/9 = 4-1.333333333333 +0.8-0.571428 +0.4444444= 3.33968254. Write a program that prompts the user to enter the number of terms which will be used to calculate Pi. Also, your program should keep asking the user to re-calculate Pl as long as you wish Your program should get as close as it can to Pi- 3.141592653589793

Explanation / Answer

Below is your code....

#include<iostream>

#include<iomanip>

using namespace std;

int main() {

double PI = 3.141592653589793;

double num,denom,piCalc;

int nTerms;

char ans = 'n';

do {

cout <<" Enter number of terms for PI Calculation: " ;

cin >> nTerms;

cin.ignore();

piCalc = 0;

num = 4.0;

denom = 1.0;

for (int i = 0; i < nTerms; ++i) {

piCalc = piCalc+num/denom;

denom += 2.0;

num = -1.0 * num;

}

cout<<"Your results are: ";

cout << "Actual PI : " <<setprecision(15) <<PI << " Calculated PI : "<< setprecision(10)<<piCalc << endl;

cout << " Would you like to try another Pi calculation? (y/n): ";

cin >> ans;

}while (ans == 'Y' || ans == 'y');

cout<<" I am satisfied with this result."<<endl;

return 0;

}

Output


Enter number of terms for PI Calculation: 5
Your results are:
Actual PI : 3.14159265358979
Calculated PI : 3.33968254

Would you like to try another Pi calculation? (y/n): y

Enter number of terms for PI Calculation: 500000
Your results are:
Actual PI : 3.14159265358979
Calculated PI : 3.141590654

Would you like to try another Pi calculation? (y/n): y

Enter number of terms for PI Calculation: 50000000
Your results are:
Actual PI : 3.14159265358979
Calculated PI : 3.141592634

Would you like to try another Pi calculation? (y/n): n

I am satisfied with this result.