Pi Java Source Code Problems, need help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Pi Java Source Code Problems, need help

Threaded View

  1. #1
    Join Date
    Oct 2013
    Posts
    1

    Post 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
            // Gregory-Leibniz 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 ("Gregory-Leibniz 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.PI-pi1);
            // Determine how accurate the result was
            int count;
            while ((Math.round(difference1)) == 0 && count <16)
            {
                count ++;
                difference1 *= 10;
            }
            System.out.printf ("Gregory-Leibniz 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.PI-pi2);
            // 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 03:45 AM. Reason: added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center