Results 1 to 7 of 7
Thread: Java PI program issue
 03142012, 10:21 PM #1Member
 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:
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; 03142012 at 11:23 PM.
 03142012, 10: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.
 03142012, 10:55 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 03142012, 11:20 PM #4Member
 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.
Can you post the algorithm or show the series that you are using to do the computation?
pi = 4(11/3+1/51/7+1/91/11 ... ) and do on.Last edited by Darin; 03142012 at 11:24 PM.
 03142012, 11:23 PM #5
Re: Java PI program issue
What value do you get if you loop 200M times
 03202012, 02: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.
 03202012, 02:12 AM #7Member
 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: 09142011, 04:06 PM 
Advice On First Program (binary to decimal converter)
By HandMeTheBacon in forum New To JavaReplies: 0Last Post: 09132011, 04:35 AM 
anyone know's how to program a conversion of binarydecimal , decimalbinary
By irnie1994 in forum JCreatorReplies: 5Last Post: 08252011, 08:32 PM 
Decimal to binary, octal to decimal
By matejm1994 in forum New To JavaReplies: 3Last Post: 12262010, 10:59 AM 
Why my program cannot calculate the decimal value?
By pearllymary78 in forum New To JavaReplies: 4Last Post: 06232008, 01:52 AM
Bookmarks