Ask Experts Questions for FREE Help !
Ask
    Gazza12345's Avatar
    Gazza12345 Posts: 2, Reputation: 1
    New Member
     
    #1

    Jul 16, 2013, 07:52 AM
    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?
    taxesforaliens's Avatar
    taxesforaliens Posts: 649, Reputation: 117
    Senior Member
     
    #2

    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's Avatar
    Gazza12345 Posts: 2, Reputation: 1
    New Member
     
    #3

    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

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

A simple calculation program [ 1 Answers ]

I have to work out a simple program in visual basic, it is based on a football season containing 10 games. I have to give 3 points per win 1 point per draw and 0 point per loss. I then have to display total points after I have put in the ten games i.e 2 wins 5 draws 3 lose =11 points. Any help...

Display Program Icons [ 5 Answers ]

I have noticed with Windows XP when you go to All Programs & look at the list the icons, most are not displayed for the downloaded programs. Just a regular File folder & then the name beside. Example: Quick Time when I put my mouse over this name I see a list of 4 to the right, then I would...

A simple program [ 1 Answers ]

Hello, I am to write a computer program to express the variation of specific heat Cp of air with Temperature as a third- degree polynomial, using the data in Table A-2b, in our book. The formula of Cp isn't important, I couldn't understand how the program is supposed to express the variation of...


View more questions Search