Results 1 to 11 of 11
Thread: Helping on Calculating Pie
 02222011, 02:17 AM #1Member
 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
 02222011, 02:35 AM #2Java Code:
for ( int counter = 0; counter <= 100000; counter += 4) { infiniteSeries = ( 4 / (5 + counter))  (4 / (3 + counter )); } double pie = initial + infiniteSeries;
By the way it is PI. Pie is a pastry filled will meat or fruit.
 02222011, 02:44 AM #3Member
 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?
 02222011, 02:50 AM #4
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); } }
 02222011, 02:58 AM #5Member
 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
 02222011, 03:04 AM #6Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 6
OP how many lines of code is the body of your for loop?
 02222011, 03:11 AM #7Member
 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
 02222011, 03:13 AM #8Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 6
Java Code:for ( int counter = 0; counter <= 100000; counter += 4) infiniteSeries = infiniteSeries + ( 4 / (5 + counter))  (4 / (3 + counter )); double pi = initial + infiniteSeries;
 02222011, 03:14 AM #9Member
 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 ));
 02222011, 03:14 AM #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.
 02222011, 03:23 AM #11Member
 Join Date
 Feb 2011
 Posts
 6
 Rep Power
 0
Similar Threads

need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09292011, 09:14 PM 
No one is helping
By anithajerome in forum Suggestions & FeedbackReplies: 3Last Post: 12062010, 01:52 PM 
Helping about SWIFT MT Message
By 82rathi4u in forum Advanced JavaReplies: 1Last Post: 08072009, 01:06 AM 
Can someone give me a helping hand...
By kwesiaryee in forum Advanced JavaReplies: 1Last Post: 09172008, 07:11 PM 
Calculator Problem. Thanks for helping! ^^
By clark_sandy in forum New To JavaReplies: 3Last Post: 07062008, 04:01 PM
Bookmarks