Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: help w/ PI Math java program

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

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

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

```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. ## Re: help w/ PI Math java program

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

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

5. ## Re: help w/ PI Math java program

Should work it seems.  Reply With Quote

6. ## 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  Reply With Quote

7. ## Re: help w/ PI Math java program 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?  Reply With Quote

8. ## 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.
```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
```  Reply With Quote

9. ## 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.  Oh well. It is valid. I checked it.  Reply With Quote

10. ## Re: help w/ PI Math java program

ah..good shot man thanks  Reply With Quote