3

Here is the problem that I am trying to solve:

Write a program that calculates the value of pi using the Gregory series. The input to the program will be a decimal value called limit. The program shall proceed to calculate the value of pi by summing up the terms of the series, but only until such time that the value of the term being summed becomes less than or equal to the value of limit, at which point the program terminates the summation. Thus, the last term of the series that is added to the sum is the first term whose value is less than or equal to the value of limit. The program then prints out the calculated value of pi at that point, as well as the actual number of terms that were summed up by the calculation.

Here is what the output should look like:

Input Limit: 0.005

Calculated Value of PI: 3.1659792728432157
No. of Terms Summed: 41

My code:

class Scratch {
    public static void main(String[] args) {

        int k = 1;
        double pi = 0.0;
        for (int term = 1; term < 50000; term++){
            double currVal = 0.0;
            if (term % 2 == 0) {
                currVal = (double) -4 / k;
            } else {
                currVal = (double) 4 / k;
            }

            pi = pi + currVal;
            if(((double)4/k) < 0.005){
                System.out.println(pi);
                System.out.println(term);
                break;
            }
            k = k + 2;

        }

    } 
 }

My code's output:

3.144086415298761
401

Please let me know where did I go wrong? Any advise is greatly appreciated.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
LearningNoob
  • 662
  • 6
  • 23
  • I tried your code and came out the expected output. – Francis G Sep 13 '19 at 06:00
  • Your code uses an input limit of 0.05, which leads to the expected output. However, the output you said you got from your code is what you would get with an input limit of 0.005. – Mihir Kekkar Sep 13 '19 at 06:03
  • @Francisaskquestion Sorry, I put 0.05 instead of 0.005. I'm not sure if the given input of the question is incorrect or my code is incorrect. – LearningNoob Sep 13 '19 at 06:06
  • @MihirKekkar Yes, that is correct. I wonder if the input given in the question is incorrect or my code is missing something. – LearningNoob Sep 13 '19 at 06:07
  • Your code does not appear to be wrong, I would assume there was a typo in the input given in the question because if you use 0.05 rather than 0.005 you would get the output desired. – Mihir Kekkar Sep 13 '19 at 06:15
  • 1
    Note that instead of all your double-casts `(double) 4`, you can just write `4.0` instead. And `k = k + 2` can be written as `k += 2`, same for `pi`. – Zabuzard Sep 13 '19 at 06:19
  • Very nice newbie question, albeit I just voted to close as "typo" ;-) – GhostCat Sep 13 '19 at 06:46
  • 1
    Works fine. But why don't you use a while loop? Is a bit faster and you are not limited to any number of iterations and can get a very good aproximation of pi. – Evgeni Enchev Sep 13 '19 at 06:49

0 Answers0