Introduction
A Harshad number (also widely known as a Niven number) is refreshingly one of the simplest "special number" checks in this entire series. After everything you've learned about digit sums, it comes down to a single, straightforward divisibility test.
If you've already implemented the sum of digits earlier in this series, you're most of the way to solving this problem already, making it a great confidence-building exercise after some of the more intricate special-number checks like Armstrong or Happy numbers.
In this guide, you'll learn:
- What a Harshad (Niven) number is
- How to check it using a while loop
- How to solve it using recursion
- How to print all Harshad numbers within a range
- How to correctly handle zero and negative numbers
- Common mistakes, interview tips, best practices, and FAQs
What Is a Harshad Number? (Also Known as a Niven Number)
A Harshad number is a number that is evenly divisible by the sum of its own digits.
The name Harshad comes from Sanskrit, roughly meaning "great joy." It is also commonly known as a Niven number, named after mathematician Ivan Niven.
For example, consider 18.
Its digit sum is:
1 + 8 = 9
Now check the divisibility:
18 % 9 = 0
Since 18 is exactly divisible by 9, 18 is a Harshad number.
Now consider 19.
Its digit sum is:
1 + 9 = 10
Checking divisibility:
19 % 10 = 9
Since the remainder is not zero, 19 is not a Harshad number.
Method 1: Using a While Loop
This is the standard and most commonly used approach.
The idea is simple:
- Calculate the sum of the digits.
- Check whether the original number is divisible by that sum.
public class HarshadNumberCheck {
public static void main(String[] args) {
int num = 18;
int original = num;
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += digit;
num /= 10;
}
if (original % sum == 0) {
System.out.println(original + " is a Harshad number.");
} else {
System.out.println(original + " is not a Harshad number.");
}
}
}
How This Works
The algorithm performs two simple tasks.
First, it calculates the sum of all digits.
For the number 18:
num = 18
digit = 8
sum = 8
digit = 1
sum = 9
num = 0
Once the digit-sum loop finishes:
sum = 9
The program then performs the divisibility check:
18 % 9 == 0
Since the remainder is 0, the program concludes that 18 is a Harshad number.
Step-by-Step Trace
For num = 18:
| Iteration | Digit Extracted | Running Sum | Remaining Number |
|---|---|---|---|
| 1 | 8 | 8 | 1 |
| 2 | 1 | 9 | 0 |
After the loop:
Digit Sum = 9
18 % 9 = 0
Therefore:
18 is a Harshad number.
Output
18 is a Harshad number.
Why We Preserve the Original Number
Just like the palindrome, Armstrong, and several other digit-based problems in this series, the digit extraction process gradually destroys the original value.
For example:
18
↓
1
↓
0
After the loop finishes, num becomes 0.
If you attempted the divisibility check using num instead of the original value:
num % sum
you would actually be checking:
0 % 9
which is not the intended calculation.
To avoid this problem, the program stores the original value before digit extraction begins:
int original = num;
After calculating the digit sum, the divisibility test correctly uses:
original % sum == 0
This same pattern appears throughout many digit-manipulation programs because the extraction loop always reduces the working variable to zero.
Method 2: Using Recursion
Instead of calculating the digit sum with a loop, we can reuse the familiar recursive sum-of-digits pattern.
The recursive method computes the digit sum, after which the divisibility check remains exactly the same.
public class HarshadNumberRecursion {
static int sumOfDigits(int n) {
if (n == 0) {
return 0;
}
return (n % 10) + sumOfDigits(n / 10);
}
public static void main(String[] args) {
int num = 18;
int sum = sumOfDigits(num);
if (num % sum == 0) {
System.out.println(num + " is a Harshad number.");
} else {
System.out.println(num + " is not a Harshad number.");
}
}
}
How This Works
The recursive method repeatedly separates the last digit and adds it to the result of the remaining digits.
For 18, the recursive calls happen like this:
sumOfDigits(18)
= 8 + sumOfDigits(1)
= 8 + (1 + sumOfDigits(0))
= 8 + 1 + 0
= 9
Once the digit sum is calculated, the program performs the same divisibility check:
18 % 9 == 0
Since the remainder is zero, the program concludes that 18 is a Harshad number.
This is a clean and reusable approach that demonstrates how mastering a simple recursive helper function can solve multiple digit-based problems throughout this series.