TheAlgorithms/Java · error · IllegalArgumentException
Input must be a positive integer. Received: {n}
Error message
Input must be a positive integer. Received: {n} What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/maths/HarshadNumber.java:30
* Wikipedia</a>
*/
public final class HarshadNumber {
private HarshadNumber() {
}
/**
* Checks if a number is a Harshad number.
* A Harshad number is a positive integer that is divisible by the sum of its
* digits.
*
* @param n the number to be checked (must be positive)
* @return {@code true} if {@code n} is a Harshad number, otherwise
* {@code false}
* @throws IllegalArgumentException if {@code n} is less than or equal to 0
*/
public static boolean isHarshad(long n) {
if (n <= 0) {
throw new IllegalArgumentException("Input must be a positive integer. Received: " + n);
}
long temp = n;
long sumOfDigits = 0;
while (temp > 0) {
sumOfDigits += temp % 10;
temp /= 10;
}
return n % sumOfDigits == 0;
}
/**
* Checks if a number represented as a string is a Harshad number.
* A Harshad number is a positive integer that is divisible by the sum of its
* digits.
*
* @param s the string representation of the number to be checkedView on GitHub (pinned to fdfb9a395b)
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).
Example fix
// before
boolean result = HarshadNumber.isHarshad(n);
// after
if (n <= 0) {
return false;
}
boolean result = HarshadNumber.isHarshad(n); Defensive patterns
Strategy: validation
Validate before calling
if (n <= 0) {
return false;
}
boolean result = HarshadNumber.isHarshad(n); Type guard
static boolean isPositiveLong(long n) {
return n > 0;
} Try / catch
try {
boolean result = HarshadNumber.isHarshad(n);
} catch (IllegalArgumentException e) {
result = false; // n <= 0, not a Harshad number
} Prevention
- 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.
When it happens
Trigger: Calling isHarshad(0L), isHarshad(-18L), or any non-positive long. The check fires before any digit-sum computation.
Common situations: Processing parsed integers from user input without positivity checks. Mathematical pipelines that pass through zero or negative values. Test cases for boundary conditions.
Related errors
- Input value must be a positive integer. Input value: {number
- Input string cannot be null or empty
- Input string must be a valid integer: {s}
- Input must be non-negative
- Input 'n' is too big to give accurate result.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/0c8cda582eaf4977.
Report an issue: GitHub.