Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Approximate Pi (Display result) Simple program (https://www.askmehelpdesk.com/showthread.php?t=758455)

  • Jul 16, 2013, 07:52 AM
    Gazza12345
    Approximate Pi (Display result) Simple program
    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?
  • Jul 16, 2013, 08:06 AM
    taxesforaliens
    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.
  • Jul 16, 2013, 08:28 AM
    Gazza12345
    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

  • All times are GMT -7. The time now is 06:07 AM.