PDA

View Full Version : Approximate Pi (Display result) Simple program


Gazza12345
Jul 16, 2013, 07:52 AM
I have to write a program that displays the result of a mathematical approximation of Pi using textbooks guidelines, basic code

//Display Pi and variable of Pi

public class Pi {
public static void main(String[] args){
System.out.println(4*1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11));
System.out.println(4*1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)+(1.0/13));
}

}

Output:
3.7440115440115442
3.8209346209346213


Am I on the right track?

taxesforaliens
Jul 16, 2013, 08:06 AM
Since Pi is 3.14159.. you are a little off.
Missing a bracket:

System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)));

you could also lose the brackets around (1.0/3) etc, but that's only cosmetics.
Otherwise it's fine.

Gazza12345
Jul 16, 2013, 08:28 AM
Thanks for the response played around with it a bit and got closer to Pi

//Display Pi and variable of Pi

public class Pi{
public static void main(String[] args){
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)));
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)+(1.0/13)));
}

}

Output:
2.9760461760461765
3.2837384837384844