I am trying to write a program that calculates the value of pi from the infinite series of pi = 4 - 4/3 + 4/5 - 4/7 + 4/9...etc.
As you can tell, the numerator stays at 4, while the denominator goes up incrementally in odd numbers, if the program finds pi, or a form of it, hopefully that will be displayed. The problem is that only the first value is calculated, i am experimenting with a for loop, but it is infinite, and I am lost upon how to proceed.
package pi;
public class pi1 {
public static void main(String[] args) {
double plusminus = 0;
double top = 4;
double bot = 1;
double pi = 4;
double newbot = bot + 2;
double piequals = top/newbot;
while(plusminus == 0 || plusminus == 1){
//if (plusminus == 0 || plusminus == 1){
//for (int count = 1; count <=5; count++)
if (plusminus == 0){
pi = pi + piequals;
plusminus = plusminus + 1;
if (pi == 3.14){
System.out.println("π = 3.14?");
}
if (pi == 3.141){
System.out.println("π = 3.141?");
}
if (pi == 3.1415){
System.out.println("π = 3.1415?");
}
if (pi == 3.14159){
System.out.println("π = 3.14159?");
}
else {
System.out.println("π = " + pi);
}
if (plusminus == 1){
pi = pi - piequals;
plusminus = plusminus - 1;
if (pi == 3.14){
System.out.println("π = 3.14?");
}
if (pi == 3.141){
System.out.println("π = 3.141?");
}
if (pi == 3.1415){
System.out.println("π = 3.1415?");
}
if (pi == 3.14159){
System.out.println("π = 3.14159?");
}
else {
System.out.println("π = " + pi);
}
}
}
}
}
}