this was an assignment i had todo today posting it here for no reason.
pretty much the program uses this weird algorithm.
http://mathworld.wolfram.com/GregorySeries.html
simply does pi = 4 * (1 - (1/3) + (1/5) - (1/7).......) etc
the more times you go through this pattern the more exact you get.
ran program with these parametersCode:import java.util.Scanner; public class PiCalc { public static void main(String[] args) { Scanner keyb = new Scanner(System.in); double pi =0; double numer = 1; double denom = 1; System.out.println("Enter the Number of iterations: "); double counter = keyb.nextDouble(); while(counter>0) { pi+= numer/denom; numer*=-1; denom+=2; counter--; } pi*=4.0; System.out.println("Pi Was Calculated As: " + pi); } }
Enter the Number of iterations:
799999999
Pi Was Calculated As: 3.1415926548383024
i googled pi and found it to be
3.141592653589793238462643383279502884197169399375 10582097494459230781640628620899862803482534211706 79.....