# Helping on Calculating Pie

• 02-22-2011, 02:17 AM
bubbamcgoo
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

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
• 02-22-2011, 02:35 AM
Junky
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.
• 02-22-2011, 02:44 AM
bubbamcgoo

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?
• 02-22-2011, 02:50 AM
Junky
Quote:

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.
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.
• 02-22-2011, 02:58 AM
bubbamcgoo
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
• 02-22-2011, 03:04 AM
al_Marshy_1981
OP how many lines of code is the body of your for loop?
• 02-22-2011, 03:11 AM
bubbamcgoo
hmmmm. not sure what you are asking.

sorry.. i really am a newbie to this java thing
• 02-22-2011, 03:13 AM
al_Marshy_1981
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?
• 02-22-2011, 03:14 AM
bubbamcgoo
actually.. the for loop is only

for ( int counter = 0; counter <= 100000; counter += 4)
infiniteSeries = infiniteSeries + ( 4 / (5 + counter)) - (4 / (3 + counter ));
• 02-22-2011, 03:14 AM
Junky
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.
• 02-22-2011, 03:23 AM
bubbamcgoo
thanks. i inserted the braces. but still have the same problem with my output