Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Pi calculating help.

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pi calculating help.

    Hi im extremly new to programming, and this is a school project i need help with.
    I'm supposed to take an input from the user and calculate the value of pi using this formula: 1/1 - 1/3 + 1/5 - 1/7 etc... so on alternating between minus and plus and denominator increasing by 2 every time. If the user inputs the number 3, then it should print (1/1 - 1/3 + 1/5 - 1/7 + 1/9) *4. The idea here is that the higher the input of the user is, the higher the equation will be close to pi.

    this is what i have so far, i know its pitififul /

    import java.util.Scanner;

    public class Pi
    {
    public static void main(String[] args)
    {
    double first = 1/1;
    double second = 1/3;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter a Number to calculate how far you want the formula to perform: ");
    double count = input.nextDouble();


    }
    }



    this is literally all i have, i know i need a while loop and an if loop. any help?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Pi calculating help.

    Welcome to the forums, please read the Forum Guidelines

    i know i need a while loop and an if loop. any help?
    Do you know how to write a while loop? An if statement? See
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pi calculating help.

    anyone help?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Pi calculating help.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    Once you've been given advice, it's helpful to know if it was useful, if you explored it, tried to apply it, had no luck with it, etc. Just throwing more advice at a poster when it's not clear they're in receive mode is a waste of time. Did you have any luck creating a loop to add a series of numbers?

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    copeg (September 6th, 2014)

  6. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pi calculating help.

    i know the basics of it but i was hoping someone would walk me through the steps.

    --- Update ---

     
    import java.util.Scanner;
     
    public class Pi
    {
    public static void main(String[] args)
    {
    double first = 1/1;
    double second = 1/3;
     
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a Number to calculate how far you want the formula to perform: ");
    double count = input.nextDouble();
     
     
    }
    }


    --- Update ---

    i tried while loop but im COMPLETLY lost

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Pi calculating help.

    What pattern can you see in the series:

    1/1 - 1/3 + 1/5 - 1/7

    ?

    Once you've seen the pattern, write an algorithm that faithfully repeats the pattern to produce a result. How can a loop with a loop control variable be written to produce the pattern or series above the desired number of times? Find the pattern and try to write the loop. Post that - just the loop - when you have something to show you want us to evaluate and help you with.

  8. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pi calculating help.

    the denominator is increasing by 2, and its alternating between - and +. so i would need to multiply -1 to every number. But i dont know how to implement this as im this is like my 6th day of programming.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Pi calculating help.

    Good pattern recognition. Can you write a for loop? Any for loop? You have to transfer knowledge of the basics to application of the basics to move forward.

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pi calculating help.

    Use a variable to multiply by that has alternating values: +1 and -1.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pi calculating help.

    i dont know what a for loop does...

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pi calculating help.

    Take a look at the tutorial:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Calculating pi
    By CruelCoin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2013, 08:25 AM
  2. Help Calculating a tip
    By Sephyncloud in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 16th, 2013, 07:58 AM
  3. Calculating Pi to more digits?
    By sci4me in forum Java Theory & Questions
    Replies: 5
    Last Post: May 23rd, 2013, 01:41 PM
  4. Calculating Percentile
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2011, 08:26 PM
  5. Calculating Strings?
    By SkyAphid in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:38 PM