Hi,
I'm practicing my java programming, and from my handbook (Deitel - Java How to Program 8th Edt.) I have to write a small application that'll calculate pi to infinite decimals for 200.000 terms. Easy peasy is what I thought, but it appears something is wrong with my coding :s I can calculate it, but the result shows only zeroes behind the decimal point.
Could someone please take a look at my code and tell me what I'm doing wrong? Many thanks in advance.
Here's my code:
public class Oef19ValueOfPi {
//hb p.199
/*Calculation the value of pi: Calculate the value of pi from the infinite series
pi=4-4/3+4/5-4/7+4/9-4/11-...
Print in a table that shows the value of pi approximated by computing the first 200.000 terms of this series. How
many terms do you have to use before you first get a value that begins with 3.14159?
*/
public static void main(String[] args) {
double pi=4;
double stat;
int sign=-1;
System.out.printf("%s%20s\n","term","Value of Pi");
for(int i=3;i<=200000;i++){
if(i%2==1){
stat=4/i;
pi=pi+(sign*stat);
sign=-sign;
}
System.out.printf("%4d%,20f\n",i-2, pi);
}
}
}