Check Whether a Number Is a Harshad Number
Decide whether a number is a Harshad number by testing divisibility by its digit sum.
A Harshad (or Niven) number is divisible by the sum of its digits. Add up the digits with a loop using n % 10 and n / 10, then return true when n % digitSum == 0. For 18 the digit sum is 9 and 18 divided by 9 leaves no remainder, so 18 is a Harshad number.
Problem Statement
A Harshad number (also called a Niven number) is a positive integer that
is divisible by the sum of its own digits. Given n, decide whether it is a
Harshad number and return true or false.
For example, 18 has digit sum 1 + 8 = 9, and 18 is divisible by 9, so
18 is a Harshad number.
Input: A single positive integer n.
Output: A boolean: true if n is a Harshad number, otherwise false.
Examples
Input: 18
Output: trueDigit sum 1+8 = 9, and 18 % 9 == 0.
Input: 19
Output: falseDigit sum 1+9 = 10, and 19 % 10 == 9, not 0.
Constraints
n >= 1 and fits in a 32-bit intThe digit sum is always at least 1 for n >= 1
Think Before You Code
Reveal the questions to ask yourself first
- How do you compute the digit sum without converting the number to text?
- What guarantees the divisor (the digit sum) is never 0 for positive n?
- Which single expression decides the answer once you have the digit sum?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Compute the digit sum, then test divisibility.
- Keep the original value of
nfor the final divisibility test. - On a working copy, run a loop:
sum += copy % 10; copy /= 10;until it is 0. - Return
n % sum == 0.
For any positive n the digit sum is at least 1, so the modulo in step 3 is
always safe.
Dry Run
Walk through the example step by step
Checking n = 18:
step | copy | digit | sum
-----+------+-------+---------
1 | 18 | 8 | 0 + 8 = 8
2 | 1 | 1 | 8 + 1 = 9
end | 0 | — | 9
18 % 9 == 0 -> true
Solution
Reveal the full Java solution
public class HarshadNumber {
public static boolean isHarshad(int n) {
if (n <= 0) return false;
int copy = n;
int sum = 0;
while (copy != 0) {
sum += copy % 10;
copy /= 10;
}
return n % sum == 0;
}
public static void main(String[] args) {
System.out.println(isHarshad(18)); // true
System.out.println(isHarshad(19)); // false
}
}
The whole check reduces to one divisibility test once the digit sum is known.
Working on a copy preserves the original n for that final n % sum check.
Guarding n <= 0 keeps the definition (Harshad numbers are positive) clean and
avoids a divide-by-zero when n is 0.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Dividing the original number down and then having no value left to test divisibility.
- Forgetting to guard n = 0, which would make the digit sum 0 and divide by zero.
Edge Cases to Test
- Every single-digit number 1-9 is trivially a Harshad number (it divides itself).
- n = 10: digit sum 1, and 10 % 1 == 0, so it is a Harshad number.
Interview Follow-Ups
- How would you print all Harshad numbers in a range efficiently?
- Two consecutive Harshad numbers form a 'Harshad pair' — how would you find them?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →