I am in java intro class the for loop problem involvies comp
I am in java intro class, the for loop problem involvies computing the value of pi. I need to compute the first 400,000 terms of this series. Print every 100th value to the screen using the equation given below. I am particularly struggling with how to begin the for loop and what to write in it.
pi = 4 - (4/3) + (4/5) - (4/7) + (4/9) - (4/11)....etc
Solution
Hi friend please find my code.
Please let me know in case of any issue.
public class PiSeries {
public static void main(String[] args) {
double piValue = 0;
int count = 0;
int lowerTerm = 1;
for(int i=1; i<=400000; i++){ // iterating from 1 to 400000
if(i%2 == 1) // if i is odd, then we need to add
piValue = piValue + 4.0/lowerTerm;
else
piValue = piValue - 4.0/lowerTerm;
lowerTerm = lowerTerm + 2;
count++;
if(count == 100){ // printing each 100th term
System.out.println(piValue);
count = 0;
}
}
}
}
/*
Sampel Output:
3.1315929035585537
3.136592684838816
3.1382593295155914
3.1390926574960143
3.139592655589785
3.139925988080531
3.1401640828900845
3.1403426540780757
3.140481542821618
3.140592653839794
3.1406835628685323
3.1407593204011373
.................
.......more
*/

