
October 27th, 2013, 11:45 PM
#1
Pi Java Source Code Problems, need help
Hey guys, I'm a student of java currently and am learning to put together a PI code, this is as far as I got and need to modify some things before I complete it, can I get some help on what to do from here, any help appriciated!
Code:
import java.util.Scanner;
import java.text.DecimalFormat;
public class PiSourceCode
{
public static void main (String [] args)
{
Scanner k = new Scanner (System.in);
// Calculate pi using method number 1
double pi1;// calculated value of PI method 1
// GregoryLeibniz Series calculation
double t1, t2, t3, t4;
double sum3, sum4;
t1 = 4.0/1.0; t2 = 4.0/3.0; t3 = 4.0/5.0; t4 = 4.0/7.0;
sum3 = t1  t2 + t3;
sum4 = t1  t2 + t3  t4;
// calculate the average of the sum of 3 terms and the sum of 4 terms
pi1 = (sum3 + sum4)/2.0;
// Place your code to calculate pi1, method 1
System.out.printf ("GregoryLeibniz Method\nPI = %17.16f\n", pi1);
System.out.printf ("System Value\nPI = %17.16f\n",Math.PI);
//Determine difference between computed PI and system value for PI
double difference1 = Math.abs(Math.PIpi1);
// Determine how accurate the result was
int count;
while ((Math.round(difference1)) == 0 && count <16)
{
count ++;
difference1 *= 10;
}
System.out.printf ("GregoryLeibniz is accurate to %d digits\n", count);
double pi2; //calculated value of pi, method 2
// Place your code here to calculate pi2 using method 2
// Based on Nilakantha Series
t1 = 3.0; t2 = 4.0/(2.0*3.0*4.0); t3 = 4.0/(4.0*5.0*6.0); t4 = 4.0/(6.0*7.0*8.0);
sum3 = t1 + t2  t3;
sum4 = t1 + t2  t3 + t4;
pi2 = (sum3+sum4)/2.0;
System.out.printf ("Nilakantha Series\nPI = %17.16f\n", pi2);
System.out.printf ("System Value\nPI = %17.16f\n",Math.PI);
//Determine difference between computed PI and system value for PI
difference1 = Math.abs(Math.PIpi2);
// Difference how accurate the result was
count = 0;
while ((Math.round(difference1)) == 0 && count <16)
{
count ++;
difference1 *= 10;
}
System.out.printf ("Nilakantha Series is accurate to %d digits\n", count);
}
}
Last edited by HanneSThEGreaT; November 1st, 2013 at 02:45 AM.
Reason: added code tags

October 28th, 2013, 03:30 AM
#2
Re: Pi Java Source Code Problems, need help
Originally Posted by ivantomljanovic
Hey guys, I'm a student of java currently ...
Then why do you post this question to Visual C++ Programming forum, not to the Java one?
Besides, you should use Code tags while posting code snippets!
Victor Nijegorodov

November 1st, 2013, 02:45 AM
#3
Re: Pi Java Source Code Problems, need help
[ Moved Thread To Java Forum ]

November 1st, 2013, 02:47 AM
#4
Re: Pi Java Source Code Problems, need help
Instead of saying you need help on certain things, please let us know where you get errors or strange results, so that it is easier to pinpoint the problem(s)
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
