# Thread: help w/ PI Math java program

1. Member Join Date
Oct 2010
Posts
44
Rep Power
0

## 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, 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

Java Code:
```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 cross posted thanks
http://www.javaprogrammingforums.com...html#post26927
Last edited by clemsontigers; 02-16-2011 at 03:31 AM.  Reply With Quote

2. ## Java Code:
`int i = 100`
That assigns 100 to i. What do you expect the rest of the line of code to do?  Reply With Quote

3. Member Join Date
Oct 2010
Posts
44
Rep Power
0

## 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

Java Code:
```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  Reply With Quote

4. Member Join Date
Oct 2010
Posts
44
Rep Power
0

## alright i updated my code w/ a for loop and believe im getting some where

Java Code:
```public class Assign3_Roberts {
public static void main(String[] args)
{
for (int i = 100; i <= 3300; 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
Last edited by clemsontigers; 02-16-2011 at 07:18 PM.  Reply With Quote

5. ##  Originally Posted by clemsontigers 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
So the maximum value of i should go up; as you can see from your output the series slowly goes down (as your assignment text already told you). The series has a very slow convergence so you should add more terms to reach a value lower than 3.1419.

kind regards,

Jos  Reply With Quote

6. Member Join Date
Oct 2010
Posts
44
Rep Power
0

## ok well i changed it to 3300 and updated it in my previous post and here is my output

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
When i is 1100 PI is 3.14250091861592
When i is 1200 PI is 3.142425292912591
When i is 1300 PI is 3.1423612929843348
When i is 1400 PI is 3.1423064293732548
When i is 1500 PI is 3.142258876034187
When i is 1600 PI is 3.1422172631478604
When i is 1700 PI is 3.1421805430157748
When i is 1800 PI is 3.1421479006319486
When i is 1900 PI is 3.14211869248028
When i is 2000 PI is 3.1420924036835256
When i is 2100 PI is 3.142068617389586
When i is 2200 PI is 3.1420469925031944
When i is 2300 PI is 3.1420272472242043
When i is 2400 PI is 3.1420091466995945
When i is 2500 PI is 3.141992493637787
When i is 2600 PI is 3.141977121088081
When i is 2700 PI is 3.1419628868240537
When i is 2800 PI is 3.1419496689300788
When i is 2900 PI is 3.1419373623006863
When i is 3000 PI is 3.1419258758397897
When i is 3100 PI is 3.141915130201854
When i is 3200 PI is 3.14190505595643
When i is 3300 PI is 3.1418955920863283  Reply With Quote

7. ##  Originally Posted by clemsontigers ok well i changed it to 3300 and updated it in my previous post and here is my output

...
When i is 3300 PI is 3.1418955920863283
Are you sure the test fails for i == 3298?

kind regards,

Jos  Reply With Quote

8. Member Join Date
Oct 2010
Posts
44
Rep Power
0

## i believe i only go up in increments of 100 b/c when i entered 3298 it only gave me the output of 100-3200  Reply With Quote

9. Member Join Date
Oct 2010
Posts
44
Rep Power
0

## it never showed the output of what the value of I is for PI to = 3.1419 or lower  Reply With Quote

10. ##  Originally Posted by clemsontigers it never showed the output of what the value of I is for PI to = 3.1419 or lower

Java Code:
```while (pi > 3.1419)[COLOR="Red"];[/COLOR]
System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i);```
What do you think the semicolon after the while loop is doing?

Goldest  Reply With Quote

11. Member Join Date
Feb 2011
Posts
7
Rep Power
0

## There's a mismatch in '{' and '}' to print the statement "value of i to give PI .."

public static void main(String[] args)
{
for (int i = 100; i <= 3300; 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);
}
} < -- Remove this

//next section
//determine value of i

int i = 0;
double pi = 100;
{ < -- Remove this
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);
} < -- Remove this
}  Reply With Quote

12. ## I kinda misread that as a start of while loop, rather than end of do-while loop. Sorry, but I need some coffee :(

Goldest  Reply With Quote

13. ##  Originally Posted by clemsontigers i believe i only go up in increments of 100 b/c when i entered 3298 it only gave me the output of 100-3200
That's what the 'enhancemant' your assignment described is all about. You should add terms (and increment i) as long as your approximation of PI is larger than that value. Other replies already showed you how it can be done.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

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