Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: help... is this right? need some1 to clarify...

1. help... is this right? need some1 to clarify...

```double denom = 3;
double pi;
double x = -1;
int i;
double pi2 =0;
double denom2 = 0;
double pi3 = 0;

pi = 4*(1-((double)1/denom));
pi2 = pi;

for (i=2; i<=300000 ; i++){

pi3 = pi2;
x = x*-1;
denom = (denom+2);
denom2 = x*((double)(1/denom));
pi2 = pi3 + denom2;

if (i%1000 == 0)
System.out.println(i + "   " + pi2);

}

}
}```

this code supposed to calculate this algorithm

4*(1-(1/3)+(1/5)-(1/7)+(1/9).....)

which is equal to pi.. but i dunno if this is right or what... it works but can somebody clarify?

2. Re: help... is this right? need some1 to clarify...

it works but can somebody clarify?
Please describe what clarification is needed. If it works and you don't understand...when all else fails, grab a pen and pencil and walk through the algorithm bit by bit (maybe not 30,000 times, but perhaps lower that loop value and println the output every loop, and check against the pen and pencil).

3. Re: help... is this right? need some1 to clarify...

it works my calculation works fine but when i run the program it doesnt come close to the value of pi which is 3.14 sumthing only up to 2.7 sumthing..

am i doing the calculation wrong?

i did... 4*(1-(1/3)) 1st then the result will be either added or subracted by an increasing denom by 2.....

is that the proper way?