-
To start off, you need something to hold the sum, you need something to hold the current value of the denominator, and you need something to store the current sign. Taking all of that gets us:
Code:
double sum = 0.0;
double denominator = 1.0;
double sign = 1.0;
Now you also need your loop:
Code:
for( int i = 0; i < 20; ++i )
{
// What goes here?
}
In the body of the loop you need to increment the sum by the current term (sign multiplied by 4.0 divided by the value of the denominator). Then, you need to increment the denominator by 2, and multiply the sign by -1 so that it flips from positive to negative on each loop iteration. You also need to print the current value of the sum.
-
thanks again
hk_mp5kpdw
look at what i did so far
Code:
#include <iostream>
using namespace std;
int main()
{
double j=0;
double pi=4;
for (i=3; i < 20; i = i + 2)
if (j%2==0)
{
pi= pi - 4. /i;
}
else ( pi= pi+4. /i;
)
cout <<pi<<endl;
return 0;
}
what's my problem
-
> what's my problem
Does it run?
Does it produce the expected output?
> if (j%2==0)
But all your j values are odd, since you start at 3 and add 2 each time.
You need some other method for alternating the + and -