Is there an easier way to do this problem instead of making a forloop and if-els
ID: 3617421 • Letter: I
Question
Is there an easier way to do this problem instead of making a forloop and if-else statement for each number up to 1500?Here's what I did....Only for 100
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double x = 0;
for ( int i = 1; i <= 100; i++ )
{
if (i % 2 == 0)
x = x - (4.0/2*i-1);
else
x = x + (4.0) / (2 * i - 1);
}
if (i % 100 == 0 )
cout << "pi with" << i << "terms = " <<static_cast<double> (x) << cout end1;
}
Here's the Question....
Calculate the value of pi from the infinite series
pi = 4 - (4/3) + (4/5) - (4/7) + (4/9) - ...
Print a table that shows the approximate value of pi for
100, 200, 300, 400, ..., 1500 terms of the series.
Expected Output:
pi with 100 terms = 3.131592903558552
pi with 200 terms = 3.136592684838814
pi with 300 terms = 3.138259329515588
pi with 400 terms = 3.139092657496011
pi with 500 terms = 3.139592655589782
pi with 600 terms = 3.139925988080528
pi with 700 terms = 3.140164082890081
pi with 800 terms = 3.140342654078072
pi with 900 terms = 3.140481542821614
pi with 1000 terms = 3.140592653839790
pi with 1100 terms = 3.140683562868528
pi with 1200 terms = 3.140759320401133
pi with 1300 terms = 3.140823422934350
pi with 1400 terms = 3.140878367966611
pi with 1500 terms = 3.140925986997196
Explanation / Answer
please rate - thanks #include #include using namespace std; int main() {double pi=0; cout.precision(15); int i,j,flip=1,terms,den=1; for(terms=100;termsRelated 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.