Write code in C++ Calculate the value of pi from the infinite series pi = 4 - 4/
ID: 3824339 • Letter: W
Question
Write code in C++
Calculate the value of pi from the infinite series pi = 4 - 4/3 + 4/5 = 4/7 + 4/9 - 4/11 + ... Print a table that shows the approximate value of pi after each of the first 1,000 terms of the series .Use for loop. Use float type for pi and any variables having decimals, and integer for non-decimal variables. Use cstdlib header function library and apply the following code before console output of pi;//Code to set the precision to 8 decimal places* cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(8);Explanation / Answer
#include <iostream>
using namespace std;
int main() {
int term;
double pi = 0;
double num = 4.0;
double den = 1.0;
cout<<"Enter how many terms:"<<endl;
cin>>term;
int i;
for(i = 1; i < term; i++)
{
double x = (num / den);
if( (i % 2) == 0)
pi -= x;
else
pi += x;
den += 2.0;
}
cout << " pi = " << pi << " ";
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.