Results 1 to 7 of 7
  1. #1
    Darin is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Java PI program issue

    Hi all, im new here and new to java.

    I have wrote this program for my hw assignment but it seems to only be accurate to the nth place after the decimal.
    My instructor has given me this answer

    Please enter n value: 10000
    Appoximate value of pi is: 3.1415826535897198

    and i get:

    Please enter n value: 10000
    Appoximate value of pi is: 3.1415726535897814



    Here is my program. Hope this is how i am suppose to post here:

    Java Code:
    //this program finds the aprox. value of pi from a user number
    
    import java.util.Scanner;
    
    public class Pi
    {
        public static void main (String[] args)
        {
            //create a scanner
            Scanner keyboard = new Scanner(System.in);
        
            int user; //user number
            double sum = 0;      //sum of fractions
            double fraction; //decimal value
            int i;     //loop variable
            double sign = 1;   // alt. signs  
             
       
           System.out.print("Please enter n value: ");   //ask user for integer
           user = keyboard.nextInt();   //asign userNum a value 
        
            for (i = 1; i <= user; i+=2)     //loop parameters
           {
                fraction = 1.0/i;      //gets fractions by using i as denominator
                sum = sum + sign * fraction;  //adds fractions to sum
                sign = sign * (-1);    //changes sign of fraction
           }
           
            System.out.print("Appoximate value of pi is: " + sum * 4);   //display answer
        
        }
    
    }
    Anyone have an idea on how to fix this issue?

    Thanks in advance.
    Last edited by Darin; 03-14-2012 at 10:23 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Java PI program issue

    Can you post the algorithm or show the series that you are using to do the computation?

    Why are you using a double to control the number of loop iterations?

    Change the coloring of the post. The off yellow is unreadable.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java PI program issue

    That series has a very, very slow convergence; see what happens if you add 1,000,000 terms or so. Do you get a better approximation? b.t.w. your code seems to look correct to me (I didn't give it a very close look, but note that the value of 'user' isn't the number of terms added).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Darin is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java PI program issue

    Sorry JoshAH, this is the best i know how to do.

    Norm,

    I fixed the issues you have addressed with no avail. It still show different numbers at the end.

    Can you post the algorithm or show the series that you are using to do the computation?
    Im not sure what you mean by this. I have not yet learned about algorithms? But ill try to explain, I used the for loop to count all demonimators, then used the sum; fraction; and sign to perform the calculations.

    pi = 4(1-1/3+1/5-1/7+1/9-1/11 ... ) and do on.
    Last edited by Darin; 03-14-2012 at 10:24 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Java PI program issue

    What value do you get if you loop 200M times

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Java PI program issue

    You must have a very slow computer to have taken so long.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Darin is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java PI program issue

    Sorry, ill be more prompt next time.

Similar Threads

  1. Replies: 7
    Last Post: 09-14-2011, 03:06 PM
  2. Advice On First Program (binary to decimal converter)
    By HandMeTheBacon in forum New To Java
    Replies: 0
    Last Post: 09-13-2011, 03:35 AM
  3. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  4. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  5. Why my program cannot calculate the decimal value?
    By pearllymary78 in forum New To Java
    Replies: 4
    Last Post: 06-23-2008, 12:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •