Results 1 to 4 of 4

Thread: Calculating Pi

  1. #1
    Simon4360 is offline Member
    Join Date
    Mar 2013
    Rep Power

    Default Calculating Pi


    New to the forum! Gotten a bit stuck on a bit of coding i'm doing for a class, wondered if you could help me at all.

    The program is supposed to generate two random numbers and input them to the equation X^2+Y^2, this can then used to calculate Pi.
    Anyways, the code works and does generate a value close to pi, i am now trying to adapt it so that the user can input the number of random trials so it
    reaches a desired accuracy, which is where i run into a problem. If you where to enter 10 trials, then 100, then a thousand it doesn't simple calculate pi for this many trials. The third entry would calculate pi for 1110 trials. Is there anyway i can get it to "forget" the number of trials the user enters at the end of each loop? Here is the code i have so far:
    PHP Code:
    import java.util.Random; 
    class Week5Part4
            static BufferedReader keyboard = new BufferedReader (new InputStreamReader(;
            static PrintWriter screen = new PrintWriter( System.out, true);
            static Random value = new Random();
            private static double p(double X, double Y)
                return ((X*X)+(Y*Y));
            public static void main (String [] args ) throws IOException
                 screen.println("The value of Pi stored in the Math class is " + Math.PI);
                 double sum = 0;
                 double overflow = 0;
                 int trials;
                 double nextX;
                 double nextY;
                        screen.println(" Enter the number of trials needed");
                        trials = new Integer(keyboard.readLine()).intValue();
                        for (int n =0; n<=trials-1; n++)
                            nextX = value.nextDouble();
                            nextY = value.nextDouble();
                            if (p(nextX,nextY)<1) sum++; 
                            if (p(nextX,nextY)>=1) overflow++;
                        screen.println("The value of Pi is " + ((4*sum)/trials) + " calculated by the program");  
                        screen.println("sum " + sum + " Overflow " + overflow + " Total " + (sum+overflow));
                 while(trials != 1 ); 
    Thanks for any help in advance!

    PS sorry if this is on the wrong section!
    Last edited by Simon4360; 03-04-2013 at 12:24 PM.

  2. #2
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Islamabad, Pakistan
    Rep Power

    Default Re: Calculating Pi

    Java Code:
    for (int n =0; n<=trials-1; n++)
    one advice why dont you run your loop n<trials..... it will execute your program efficiently .

    Secondly i think do terminate your while loop
    Ask user to press 1 to continue or 2 to terminate
    On 1 program should simply work
    On 2 program should terminate

    Coz you are always doing
    Java Code:
    It will cause program to run infinitely.


  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Calculating Pi

    Print out the value of 'trials' just before the for loop.
    I don't spot where it could be adding previous trials values.

    Ah, it's not trials that's the don't reset any of your other variables (sum, overflow, nextX, nextY).
    The easiest way to avoid this is to declare your variables in the narrowest scope possible, in this case those 4 should be declared inside the while loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Simon4360 is offline Member
    Join Date
    Mar 2013
    Rep Power

    Default Re: Calculating Pi

    Ahhh Tolls, thank you so much! program works perfectly now. Such a simple solution :)

Similar Threads

  1. calculating the mean
    By jimbao in forum New To Java
    Replies: 8
    Last Post: 12-14-2012, 01:40 AM
  2. Calculating percent
    By Dankaru in forum New To Java
    Replies: 3
    Last Post: 11-24-2012, 09:04 PM
  3. need help with calculating something
    By mikec420 in forum New To Java
    Replies: 13
    Last Post: 09-29-2011, 09:14 PM
  4. Help in calculating time different!
    By rocky86 in forum New To Java
    Replies: 6
    Last Post: 10-11-2010, 04:18 PM
  5. calculating with dates
    By hannes in forum New To Java
    Replies: 1
    Last Post: 01-14-2010, 08:22 PM

Posting Permissions

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