{"record":{"id":"0c8cda582eaf4977","repo":"TheAlgorithms/Java","slug":"input-must-be-a-positive-integer-received-n","errorCode":null,"errorMessage":"Input must be a positive integer. Received: {n}","messagePattern":"Input must be a positive integer\\. Received: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/HarshadNumber.java","lineNumber":30,"sourceCode":" *      Wikipedia</a>\n */\npublic final class HarshadNumber {\n    private HarshadNumber() {\n    }\n\n    /**\n     * Checks if a number is a Harshad number.\n     * A Harshad number is a positive integer that is divisible by the sum of its\n     * digits.\n     *\n     * @param n the number to be checked (must be positive)\n     * @return {@code true} if {@code n} is a Harshad number, otherwise\n     *         {@code false}\n     * @throws IllegalArgumentException if {@code n} is less than or equal to 0\n     */\n    public static boolean isHarshad(long n) {\n        if (n <= 0) {\n            throw new IllegalArgumentException(\"Input must be a positive integer. Received: \" + n);\n        }\n\n        long temp = n;\n        long sumOfDigits = 0;\n        while (temp > 0) {\n            sumOfDigits += temp % 10;\n            temp /= 10;\n        }\n\n        return n % sumOfDigits == 0;\n    }\n\n    /**\n     * Checks if a number represented as a string is a Harshad number.\n     * A Harshad number is a positive integer that is divisible by the sum of its\n     * digits.\n     *\n     * @param s the string representation of the number to be checked","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/HarshadNumber.java#L12-L48","documentation":"Thrown by HarshadNumber.isHarshad(long n) when n is less than or equal to 0. A Harshad (or Niven) number is a positive integer divisible by the sum of its digits, so zero and negatives are excluded by definition. The method computes the digit sum in a while loop and divides n by that sum, so non-positive input would either be meaningless or cause division by zero (digit sum of 0). The error message includes the received value.","triggerScenarios":"Calling isHarshad(0L), isHarshad(-18L), or any non-positive long. The check fires before any digit-sum computation.","commonSituations":"Processing parsed integers from user input without positivity checks. Mathematical pipelines that pass through zero or negative values. Test cases for boundary conditions.","solutions":["Ensure n > 0 before calling isHarshad(long).","Validate parsed user input for positivity at the source.","Handle zero and negative inputs with domain-appropriate logic (return false or throw a custom exception)."],"exampleFix":"// before\nboolean result = HarshadNumber.isHarshad(n);\n\n// after\nif (n <= 0) {\n    return false;\n}\nboolean result = HarshadNumber.isHarshad(n);","handlingStrategy":"validation","validationCode":"if (n <= 0) {\n    return false;\n}\nboolean result = HarshadNumber.isHarshad(n);","typeGuard":"static boolean isPositiveLong(long n) {\n    return n > 0;\n}","tryCatchPattern":"try {\n    boolean result = HarshadNumber.isHarshad(n);\n} catch (IllegalArgumentException e) {\n    result = false; // n <= 0, not a Harshad number\n}","preventionTips":["Ensure n > 0 before calling isHarshad(long).","Validate parsed numeric input for positivity before use.","Return false for non-positive inputs in your domain logic."],"tags":["math","harshad","number-theory","argument-validation","positive-integer"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}