Yes that code uses different signs.
But the whole of the code does not follow the equation and produce the correct answer.
Add a println after each of those assignments to print out the value of var.
Shorten the loop to 10 times and look at the output

Yes that code uses different signs.
But the whole of the code does not follow the equation and produce the correct answer.
Add a println after each of those assignments to print out the value of var.
Shorten the loop to 10 times and look at the output

wow when i shortened the loop the numebr got bigger

Throw out that program and start over. You have 90% of what you need for the program.

Look again at the terms that are being added each loop.
There are 2 things that change every iteration.

``````public class computingPi {
public static void main(String args[]){
double var = 0,  pi;

pi = 0;

for(int count = 10000; count<=100000;count= count + 10000){
for (int i =1; i<=count;i+=2){
var +=(1.0/(2.0*i-1.0)-1.0/(2.0*i+1.0));

}
pi = 4* var;

}

}

}``````

this still doesnt work not the outer for loop changes the the count

I don't know why you are trying to use two loops. One will do it.

Try having the code in your loop add a single term to the sum each time.
The terms to be added are as I've posted many times:
+4/1
-4/3
+4/5
-4/7
...

If you generalize the above terms you'd get:
sign * 4 / denominator

Besides summing these terms one per loop, you need to add 2 to the denominator and change the sign.

I don't know why you are trying to use two loops. One will do it.

Try having the code in your loop add a single term to the sum each time.
The terms to be added are as I've posted many times:
+4/1
-4/3
+4/5
-4/7
...

If you generalize the above terms you'd get:
sign * 4 / denominator

Besides summing these terms one per loop, you need to add 2 to the denominator and change the sign.

only reason im having two loops is cuz teacher wants value of pi when count =10000,20000,30000,40000.....100000
alright let me try that

You could have an if test inside of the loop to print the current value of pi when the loop counter is at those numbers.

yes i got it thank you

``````public class computingPi {
public static void main(String args[]){
double var = 0;

for (int i =0; i<=100000;i++){
var += (Math.pow((-3), (-i)))/(2*i + 1) ;

if (i % 10000 == 0) {
System.out.println("after the  count is "+i+" PI is   " + Math.sqrt(12)*var);

}
}

}

}``````

Where did you get that equation from?
It doesn't look anything like the one you posted earlier.

It surely doesn't follow this series:
pi = 4(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11_+...+(1/(2i +1))-(1/(2i +1))

from wiki but it works

But it doesn't do what your assignment says to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.