public class Prog5_25 { public static void main(String[] args) { System.out.println("This program will compute the value of pi through an estimation of the form 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ..."); int factor = 1; // we will multiply factor by -1 so that it alternates adding and subtraction 1 / i to our value double pi = 0.0; for(int i=1;i<1000;i+=2) // iterate by 2's { pi += factor * 1.0 / i; // add next fraction 1 / i to pi, alternating + or - using factor factor *= -1; // change factor from plus to minus or minus to plus } pi *= 4; // finish off computation by multiplying by 4 System.out.println("Estimate for pi is " + pi); System.out.println("Actual value for pi is " + Math.PI); } }