# help w/ PI Math java program

• February 15th, 2011, 09:42 PM
robertsbd
help w/ PI Math java program
im currently in entry level java class and need help with a code..here are my instructions

Mathematicians define the constant PI to be
PI = 4 *( 1 - 1/(2*2-1) + 1/(2*2+1) - 1/(2*4-1) + 1/(2*4+1) - 1/(2*6-1) + 1/(2*6+1)- . . . .format . . . . . - 1/(2*i-1) + 1/(2*i+1) )
Note that i starts at 2 and is incremented by 2. The higher the i, the more precise PI will be. Also note that the higher the value of i, the smaller the value of PI.

Write a program that displays the value of PI for i = 100, 200, 300, . . ,1000. Output should be something like this :
when i is 100 PI is xxxx
when i is 200 PI is xxxx
etc . .
when i is 900 PI is xxxx
when i is 1000 PI is xxxx

Once you have this working, enhance the program, add another section. The next section of the program should determine the value of i needed to achieve a value of PI that is equal to or slightly smaller than 3.1419. The program should output the following message :
value of i to give PI equal or smaller than 3.1419 is xxxxxx

and here is my code so far, i underlined the part where im getting an errors
should i put each value of PI in an if statement?? im kinda stuck right now thanks

Code java:

```public class ConstantPI { public static void main(String[] args) {     [U]int i = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;[/U] double pi4 = 1.0; for(int j = 2; j <= i; j += 2) pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1); double pi = 4.0*pi4; System.out.println("When i is " + i + " PI is " + pi); }   }```

btw crossposted thanks

help w/ PI Math java program - Java Forums
• February 16th, 2011, 11:49 AM
robertsbd
Re: help w/ PI Math java program
alright here is my updated code but still not sure how to calculate the pi when i =200,300, etc

also i tried to work on the 2nd part of my code

Code java:

```public class Assign3_Roberts { public static void main(String[] args) {     int i = 100; double pi4 = 1.0; for(int j = 2; j <= i; j += 2) pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1); double pi = 4.0*pi4; System.out.println("When i is " + i + " PI is " + pi); }       //next section //value of i int i = 0; double pi = 100; { do { i ++; double pi4 = 1.0; for(int j = 2; j <= i; j += 2) pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1); pi = 4.0*pi4; } while (pi > 3.1419); System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i); } }```

and when i run the program my output is:

When i is 100 PI is 3.1514934010709914
• February 16th, 2011, 11:58 AM
robertsbd
Re: help w/ PI Math java program
alright i updated my code w/ a for loop and believe im getting some where

Code java:

```public class Assign3_Roberts { public static void main(String[] args) { for (int i = 100; i <= 1000; i += 100) { double pi4 = 1.0; for(int j = 2; j <= i; j += 2) pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1); double pi = 4.0*pi4; System.out.println("When i is " + i + " PI is " + pi); } }   //next section //determine value of i   int i = 0; double pi = 100; { do { i ++; double pi4 = 1.0; for(int j = 2; j <= i; j += 2) pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1); pi = 4.0*pi4; } while (pi > 3.1419); System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i); } }```

and output is now:
When i is 100 PI is 3.1514934010709914
When i is 200 PI is 3.1465677471829556
When i is 300 PI is 3.1449149035588526
When i is 400 PI is 3.144086415298761
When i is 500 PI is 3.143588659585789
When i is 600 PI is 3.143256545948974
When i is 700 PI is 3.1430191863875865
When i is 800 PI is 3.142841092554028
When i is 900 PI is 3.1427025311614294
When i is 1000 PI is 3.1425916543395442

but not getting the 2nd section right
• February 16th, 2011, 03:55 PM
javapenguin
Re: help w/ PI Math java program
Should work it seems.
• February 16th, 2011, 07:41 PM
robertsbd
Re: help w/ PI Math java program
but it never showed the output of what the value of I is for PI to = 3.1419 or lower
• February 16th, 2011, 08:00 PM
javapenguin
Re: help w/ PI Math java program
Quote:

Originally Posted by robertsbd
but it never showed the output of what the value of I is for PI to = 3.1419 or lower

Sorry. I was looking at a glance then. I had to be somewhere right after I wrote that post.

BTW, what are you getting for the second section?
• February 16th, 2011, 08:12 PM
javapenguin
Re: help w/ PI Math java program
Hmmmmmm....I think I mighta found the problem.

You had some extra brackets that you didn't need.
Code java:

```public class Assign3_Roberts { public static void main(String[] args) { for (int i = 100; i <= 1000; i += 100) { double pi4 = 1.0; for(int j = 2; j <= i; j += 2) pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1); double pi = 4.0*pi4; System.out.println("When i is " + i + " PI is " + pi);   }   //next section //determine value of i   int i = 0; double pi = 100;   do { i ++; double pi4 = 1.0; for(int j = 2; j <= i; j += 2) pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1); pi = 4.0*pi4; } while (pi > 3.1419); System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i); } }```

```

----jGRASP: operation complete.

----jGRASP exec: java Assign3_Roberts

When i is 100 PI is 3.1514934010709914
When i is 200 PI is 3.1465677471829556
When i is 300 PI is 3.1449149035588526
When i is 400 PI is 3.144086415298761
When i is 500 PI is 3.143588659585789
When i is 600 PI is 3.143256545948974
When i is 700 PI is 3.1430191863875865
When i is 800 PI is 3.142841092554028
When i is 900 PI is 3.1427025311614294
When i is 1000 PI is 3.1425916543395442
value of i to give PI equal or smaller than 3.1419 is 3254
```
• February 16th, 2011, 08:24 PM
javapenguin
Re: help w/ PI Math java program
I'm still baffled as to how that lower for loop even works since it appears that i is less than j from the beginning.

:confused: :confused:

Oh well. It is valid. I checked it.
• February 16th, 2011, 08:43 PM
robertsbd
Re: help w/ PI Math java program
ah..good shot man thanks