Results 1 to 7 of 7
Thread: Java PI program issue
- 03-14-2012, 09:21 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Java PI program issue
Hi all, im new here and new to java.
I have wrote this program for my hw assignment but it seems to only be accurate to the nth place after the decimal.
My instructor has given me this answer
Please enter n value: 10000
Appoximate value of pi is: 3.1415826535897198
and i get:
Please enter n value: 10000
Appoximate value of pi is: 3.1415726535897814
Here is my program. Hope this is how i am suppose to post here:
Anyone have an idea on how to fix this issue?Java Code://this program finds the aprox. value of pi from a user number import java.util.Scanner; public class Pi { public static void main (String[] args) { //create a scanner Scanner keyboard = new Scanner(System.in); int user; //user number double sum = 0; //sum of fractions double fraction; //decimal value int i; //loop variable double sign = 1; // alt. signs System.out.print("Please enter n value: "); //ask user for integer user = keyboard.nextInt(); //asign userNum a value for (i = 1; i <= user; i+=2) //loop parameters { fraction = 1.0/i; //gets fractions by using i as denominator sum = sum + sign * fraction; //adds fractions to sum sign = sign * (-1); //changes sign of fraction } System.out.print("Appoximate value of pi is: " + sum * 4); //display answer } }
Thanks in advance.Last edited by Darin; 03-14-2012 at 10:23 PM.
- 03-14-2012, 09:49 PM #2
Re: Java PI program issue
Can you post the algorithm or show the series that you are using to do the computation?
Why are you using a double to control the number of loop iterations?
Change the coloring of the post. The off yellow is unreadable.
- 03-14-2012, 09:55 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Java PI program issue
That series has a very, very slow convergence; see what happens if you add 1,000,000 terms or so. Do you get a better approximation? b.t.w. your code seems to look correct to me (I didn't give it a very close look, but note that the value of 'user' isn't the number of terms added).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-14-2012, 10:20 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Re: Java PI program issue
Sorry JoshAH, this is the best i know how to do.
Norm,
I fixed the issues you have addressed with no avail. It still show different numbers at the end.
Im not sure what you mean by this. I have not yet learned about algorithms? But ill try to explain, I used the for loop to count all demonimators, then used the sum; fraction; and sign to perform the calculations.Can you post the algorithm or show the series that you are using to do the computation?
pi = 4(1-1/3+1/5-1/7+1/9-1/11 ... ) and do on.Last edited by Darin; 03-14-2012 at 10:24 PM.
- 03-14-2012, 10:23 PM #5
Re: Java PI program issue
What value do you get if you loop 200M times
- 03-20-2012, 01:10 AM #6
Re: Java PI program issue
You must have a very slow computer to have taken so long.
If you don't understand my response, don't ignore it, ask a question.
- 03-20-2012, 01:12 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
I am making a program that format the output with two decimal places..
By antidup in forum New To JavaReplies: 7Last Post: 09-14-2011, 03:06 PM -
Advice On First Program (binary to decimal converter)
By HandMeTheBacon in forum New To JavaReplies: 0Last Post: 09-13-2011, 03:35 AM -
anyone know's how to program a conversion of binary-decimal , decimal-binary
By irnie1994 in forum JCreatorReplies: 5Last Post: 08-25-2011, 07:32 PM -
Decimal to binary, octal to decimal
By matejm1994 in forum New To JavaReplies: 3Last Post: 12-26-2010, 09:59 AM -
Why my program cannot calculate the decimal value?
By pearllymary78 in forum New To JavaReplies: 4Last Post: 06-23-2008, 12:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks