roughosing
roughosing

Reputation: 71

Computing Pi in Java

This weeks assignment in programming is to compute Pi in java using this as the basis for the assignment:

  1. (for 80% of the marks): USING A WHILE OR A DO-WHILE LOOP write a program to compute PI using the following equation:
    PI = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + ...
    Allow the user to specify the number of terms (5 terms are shown) to use in the computation.
    Each time around the loop only one extra term should be added to the estimate for PI.
  2. (for 20% of the marks): Alter your solution from part one so that the user is allowed to specify the precision required between 1 and 8 digits (i.e. the number of digits which are correct; e.g. to 5 digits PI is 3.14159), rather than the number of terms. The condition on the loop should be altered so that it continues until the required precision is obtained. Note that you need only submit this second version of the program (assuming you have it working).

I'm only able to use the above method to compute Pi, as thats what the lecturer wants. Ive got this so far, although the code keeps giving me the same wrong answer for every even number and a different wrong answer for each odd number. Im still on part one as i havent got the right answer yet to be able to progress onto part 2.

All help would be great, as the program needs to be submitted by tueday. Thanks in advance!

import java.util.Scanner;

public class ComputePI {

    public static void main(String[] args) {

        System.out.print( "Please enter the amount of decimal "
                + "digits of PI, you would like to set it too");
        Scanner termScan = new Scanner( System.in );
        int term = termScan.nextInt();
        termScan.close();
        double pi = 3.0;
        int loopCount = 2;
        int number = 2;


            while ( loopCount <= term )
            {
                if (loopCount % 2 == 0)
                {
                    pi = pi + ( 4.0/ ((number) * (number+1) * (number+2)) );
                }
                else 
                {
                    pi = pi - ( 4.0 / ((number) * (number+1) * (number+2)) );
                }

                number = number + 2;

                loopCount++;
            }

        System.out.print( "The Value of Pi in " + term +
                " terms is equal to " + pi);
    }

}

Upvotes: 1

Views: 1957

Answers (2)

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

I am not going to give you code (you can figure it out for yourself, I'm certain), but I'll give you the location for where to look for the problem.

In the negative terms, you are adding 2 to each number multiplied together. However, you are adding 2 to each number in every iteration of the loop: the numberXXX + 2 part should probably just be numberXXX.

You are now also incrementing the numberXXX variables when loopCount is 1. In fact, the if (loopCount == 1) part is unnecessary, since you already initialize pi. You should just remove the if block there and switch the loopCount % 2 == X blocks around.

I'll also give you general advice about things you might want to consider in your code.

  1. You don't need constants like 4.0 to be in a variable. Just replace fourConstant with 4.0.
  2. You don't need to use an else if for the third block: if loopCount % 2 is not 0 it is definitely 1.
  3. loopCount can only get integer values, so it should probably be an int. A double just consumes extra memory (this is not too problematic here, but may be in large programs) and can in some cases lead to errors (too large numbers may cause rounding errors).
  4. You don't need three variables for numberOne, numberTwo and numberThree; they can always be represented as numberOne, numberOne + 1 and numberOne + 2.

Upvotes: 1

Dimitris Fousteris
Dimitris Fousteris

Reputation: 1111

You are incrementing the variables numerOne,numberTwo,numberThree in case the loopCount = 1. In this case you should just continue the loop without incrementing this variables. So change this:

if (loopCount == 1 )
{
    pi = 3.0;
}   

in:

if (loopCount == 1 )
{
    pi = 3.0;
    loopCount++;
    continue;
}  

And change this:

pi = pi - ( fourConstant / ((numberOne+2)*(numberTwo+2)*(numberThree+2)));

into:

pi = pi - ( fourConstant / ((numberOne)*(numberTwo)*(numberThree)));

Or you could just initialize loop count to 2 and remove the first if.

Additionally it would be better is loopCount and term were integer variables instead of Double since they are going to hold only integer values.

Upvotes: 0

Related Questions