# Thread: Helping on Calculating Pie

1. Member
Join Date
Feb 2011
Posts
6
Rep Power
0

## Helping on Calculating Pie

Hi everyone, a helping hand will surely help. I am confused with my code. I am trying to solve the question below that solves for pie. When I run the program, the output is:

The value of pie to the 200,000th term is 4.000000

This is not the right answer and I can't figure out what I did wrong. Can someone please help? Thanks a lot.

Question:
Calculate the value of π from the infinite series
π = 4 - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + ....
Print a table that shows the value of π approximated by computing one term of this series, by two terms, by three terms, and so on.

My Code:
public class CalculatingPie
{
public static void main( String[] args )
{
double initial = 4;
double infiniteSeries = 0;

for ( int counter = 0; counter <= 100000; counter += 4)
infiniteSeries = ( 4 / (5 + counter)) - (4 / (3 + counter ));

double pie = initial + infiniteSeries;

System.out.printf( "The value of pie to the 200,000th term is %f", pie);

} // end method main

} // end class CalculatingPie

2. Java Code:
```for ( int counter = 0; counter <= 100000; counter += 4) {
infiniteSeries = ( 4 / (5 + counter)) - (4 / (3 + counter ));
}
double pie = initial + infiniteSeries;```
You only add the last value of infiniteSeries. What about all the other values calculated.

By the way it is PI. Pie is a pastry filled will meat or fruit.

3. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
haha.. touche about the pie

what do you mean that i only add the last value? does the for statement create a loop and causes infiniteSeries to add all the values?

4. Originally Posted by bubbamcgoo
causes infiniteSeries to add all the values?
Please show where in your code it does that. The code below simplies what you are doing.
Java Code:
```class Test {
public static void main(String[]args) {
int value = 1;
int progress = 0;
for(int index = 2; index <= 10; index++ ) {
progress = index;
}
int sum = value + progress;
System.out.println(sum);
}
}```
The output is 11 whereas the expectation is that it should output 55.

5. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
hmmmm.. good point. thanks

i changed it as follows:

public class CalculatingPi
{
public static void main( String[] args )
{
double initial = 4; // initializes pie with 4 and uses for statement to plus/minus the infinite series
double infiniteSeries = 0;

for ( int counter = 0; counter <= 100000; counter += 4)
infiniteSeries = infiniteSeries + ( 4 / (5 + counter)) - (4 / (3 + counter ));

double pi = initial + infiniteSeries;

System.out.printf( "The value of pie to the 200,000th term is %f", pi);

} // end method main

} // end class CalculatingPi

now i get 3.000000 as the output. still very confused

6. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
5
OP how many lines of code is the body of your for loop?

7. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
hmmmm. not sure what you are asking.

sorry.. i really am a newbie to this java thing

8. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
5
Java Code:
```for ( int counter = 0; counter <= 100000; counter += 4)
infiniteSeries = infiniteSeries + ( 4 / (5 + counter)) - (4 / (3 + counter ));

double pi = initial + infiniteSeries;```
is all this code inside your for loop?

9. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
actually.. the for loop is only

for ( int counter = 0; counter <= 100000; counter += 4)
infiniteSeries = infiniteSeries + ( 4 / (5 + counter)) - (4 / (3 + counter ));

10. They were pointing out that you do not have braces around your for loop. A n00b mistake is to think they have 2 or more lines in a loop when they reaaly only have one because they left off the braces.

Rule of thumb: always use braces around loops and if statements.

11. Member
Join Date
Feb 2011
Posts
6
Rep Power
0
thanks. i inserted the braces. but still have the same problem with my output

#### Posting Permissions

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