{"record":{"id":"254872a4867ca910","repo":"TheAlgorithms/Java","slug":"please-input-integer-number-between-0-and-500","errorCode":null,"errorMessage":"Please input Integer Number between 0 and 500","messagePattern":"Please input Integer Number between 0 and 500","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/PiNilakantha.java","lineNumber":27,"sourceCode":"    // https://en.scratch-wiki.info/wiki/Calculating_Pi\n    public static void main(String[] args) {\n        assert calculatePi(0) == 3.0;\n        assert calculatePi(10) > 3.0;\n        assert calculatePi(100) < 4.0;\n\n        System.out.println(calculatePi(500));\n    }\n\n    /**\n     * @param iterations number of times the infinite series gets repeated Pi\n     * get more accurate the higher the value of iterations is Values from 0 up\n     * to 500 are allowed since double precision is not sufficient for more than\n     * about 500 repetitions of this algorithm\n     * @return the pi value of the calculation with a precision of x iteration\n     */\n    public static double calculatePi(int iterations) {\n        if (iterations < 0 || iterations > 500) {\n            throw new IllegalArgumentException(\"Please input Integer Number between 0 and 500\");\n        }\n\n        double pi = 3;\n        int divCounter = 2;\n\n        for (int i = 0; i < iterations; i++) {\n            if (i % 2 == 0) {\n                pi = pi + 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2));\n            } else {\n                pi = pi - 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2));\n            }\n\n            divCounter += 2;\n        }\n        return pi;\n    }\n}\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/PiNilakantha.java#L9-L45","documentation":"Thrown by PiNilakantha.calculatePi(int iterations) when iterations is outside [0, 500]. The Nilakantha series approximation is bounded because double precision is insufficient for more than ~500 iterations (terms vanish into rounding noise), and negative iterations are meaningless. The guard enforces both bounds before the summation loop.","triggerScenarios":"Calling calculatePi(-1), calculatePi(0) (allowed), calculatePi(500) (allowed), or calculatePi(501) — any value < 0 or > 500.","commonSituations":"User-supplied iteration count from a CLI or request payload not validated against the bound; caller assumed more iterations = more accuracy and exceeded 500; default value of -1 used as a sentinel but not handled; auto-scaling logic (e.g., iterations = precision * 100) overflowing the cap.","solutions":["Clamp iterations to [0, 500] before calling: iterations = Math.max(0, Math.min(500, requested)).","Validate at the input boundary and surface a domain-specific error for out-of-range requests.","If higher precision is genuinely needed, switch to a BigDecimal-based or arbitrary-precision pi estimator."],"exampleFix":"// before\ndouble pi = PiNilakantha.calculatePi(requestedIterations);\n\n// after\nint iters = Math.max(0, Math.min(500, requestedIterations));\ndouble pi = PiNilakantha.calculatePi(iters);","handlingStrategy":"validation","validationCode":"int iters = Math.max(0, Math.min(500, requestedIterations));\ndouble pi = PiNilakantha.calculatePi(iters);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp iteration counts to the documented [0, 500] range before calling.","Validate user-supplied iteration counts at the input boundary.","Switch to arbitrary-precision estimators if more than 500 iterations are genuinely needed."],"tags":["math","pi","invalid-argument","out-of-range"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}