Definition
n % s(n) = 0
Positive n is Harshad if it is divisible by the sum of its decimal digits.
Harshad numbers are a classic interview warm-up: digit extraction plus one divisibility test. This tutorial covers the base-10 definition, a safe C helper that avoids % 0, a live preview, algorithm steps, worked C examples, edge cases, and complexity.
n % s(n) = 0
Positive n is Harshad if it is divisible by the sum of its decimal digits.
% 10 loop
Peel digits with n % 10, accumulate, divide by 10.
No % 0
Reject n ≤ 0 so the digit sum is never used as a zero divisor.
1–20
List every Harshad value in a closed interval with the same helper.
Check n
See digit sum, remainder, and Harshad / not Harshad instantly.
Same class
Also called Niven numbers — same base-10 digit-sum rule.
A Harshad number (also called a Niven number) is a positive integer n that is divisible by the sum of its decimal digits. For 18, digits sum to 9, and 18 % 9 == 0, so 18 is Harshad.
In C interviews you are usually asked to implement a digit-sum helper, test original % sum == 0, guard against n ≤ 0, and optionally list Harshad numbers in a range.
It trains digit loops, divisibility, and careful zero guards — skills that show up in many digit-property problems (digital roots, checksums, other bases).
One digit sum, one modulus test.
Digit loop destroys n — save a copy first.
For 1..9, s(n) = n, so all are Harshad.
Reject non-positive n before dividing.
In short: for positive n, compute the sum of decimal digits s, then check n % s == 0 — that is the Harshad test.
Given a positive integer n, decide whether it is Harshad in base 10; optionally list every Harshad value in a closed interval.
/* n = 18
* digits: 1 + 8 = 9
* 18 % 9 == 0 → Harshad
*
* n = 11
* digits: 1 + 1 = 2
* 11 % 2 != 0 → not Harshad
*/ | Item | Type | Description |
|---|---|---|
n / number | int | Positive integer to classify (Example 1). |
| Range bounds | int | Inclusive interval such as [1, 20] (Example 2). |
| Result | flag / text | Harshad or not; or a printed list of Harshad values. |
function digit_sum_base10(n): // assume n >= 0
s = 0
while n > 0:
s += (n mod 10)
n = floor(n / 10)
return s
function is_harshad(n):
if n <= 0:
return false
s = digit_sum_base10(n)
if s == 0:
return false
return (n mod s) = 0 | Task | Idea | Extra space |
|---|---|---|
| Single check | Digit sum + n % s == 0 | O(1) |
| Range scan | Call the same helper for each i in [L, R] | O(1) |
| Goal | Pattern |
|---|---|
| Next digit | sum += n % 10; n /= 10; |
| Preserve n | int original = number; before the digit loop |
| Harshad test | return original % sum == 0; |
| Reject invalid | if (number <= 0) return 0; |
| Zero sum guard | if (sum == 0) return 0; before % |
Related ideas — only the one-shot digit sum is required for Harshad.
n % s(n)One digit sum, then divisibility
iterate sKeep summing until one digit — not needed here
radix bSame rule with base-b digits if the prompt asks
guard % 0State n > 0 and never divide by a zero sum
Reach for Harshad drills when digit sums and divisibility matter.
Quick check of digit loops, modulo, and edge cases.
Pairs with happy, Disarium, and other digit walks.
Digit sums appear in simple validation schemes.
List or count Harshad numbers in [L, R].
Do not keep iterating until one digit unless asked.
Key benefit: a tiny problem that covers digits, divisibility, and undefined-behavior awareness in one pass.
Enter a positive integer and see digit sum, remainder, and whether it is Harshad.
Two complete C programs — classify a single value, and list Harshad numbers in [1, 20]. Click View Output to reveal sample console results.
Digit sum + modulus for n = 18.
18Explicit guards so n % 0 never runs; keep original for the final test.
#include <stdio.h>
int digit_sum_positive(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int is_harshad(int number) {
int original = number;
int sum;
if (number <= 0) {
return 0;
}
sum = digit_sum_positive(number);
if (sum == 0) {
return 0;
}
return original % sum == 0;
}
int main(void) {
int number = 18;
if (is_harshad(number)) {
printf("%d is a Harshad number.\n", number);
} else {
printf("%d is not a Harshad number.\n", number);
}
return 0;
} For 18, the digit sum is 9. Since 18 % 9 == 0, the function returns true (nonzero int). The early returns keep the modulus from ever seeing a zero divisor.
Reuse the same helper across a closed interval.
[1, 20]Scan each i independently; listing matches the classic reference output.
#include <stdio.h>
int digit_sum_positive(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int is_harshad(int num) {
int original = num;
int sum;
if (num <= 0) {
return 0;
}
sum = digit_sum_positive(num);
if (sum == 0) {
return 0;
}
return original % sum == 0;
}
int main(void) {
int i;
printf("Harshad numbers in the range 1 to 20:\n");
for (i = 1; i <= 20; ++i) {
if (is_harshad(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
} 11, 13, 14, 15, 16, 17, and 19 fail the final modulus test; the rest in the interval pass.
Require n > 0; reject non-positive inputs before any modulus.
Save original, then walk digits with % 10 / /= 10.
If sum > 0 and original % sum == 0, report Harshad.
For 18, s = 9 and 18 % 9 == 0 — Harshad.
n = 18Trace the digit-sum loop, then the final modulus check.
| Step | n (working) | Digit | Running sum |
|---|---|---|---|
1 | 18 | 8 | 8 |
2 | 1 | 1 | 9 |
3 | 0 | — | loop ends |
4 | original 18 | — | 18 % 9 == 0 → Harshad |
By contrast, 11 has digit sum 2 and 11 % 2 != 0, so it is not Harshad.
Where Harshad thinking shows up beyond the interview prompt.
Build fluency with % 10 / /= 10 extraction.
Example: same helper reused for sum-of-digits problems.
Combine a derived value with a modulus test.
Example: n % s(n) == 0.
Teaches why dividing by a zero digit sum is undefined in C.
Example: guard n ≤ 0 and sum == 0.
List or count Harshad numbers in an interval.
Example: all Harshad in 1–20.
Generalize the digit extractor with radix b.
Example: Harshad-b numbers in contests.
Happy, Disarium, and similar digit-property checks.
Example: previous/next interview pages.
Pro Tip: if the interviewer mentions digital roots, clarify that Harshad only needs one digit sum — not iterated reduction.
Why this approach earns interview points.
Digit sum + one modulus — easy to write and explain.
Work proportional to the number of decimal digits.
One is_harshad powers single checks and range scans.
Guards for n ≤ 0 show you understand C’s % 0 hazard.
Pro Tip: lead with the definition and the zero guard — interviewers often probe the n = 0 case.
Small habits that keep Harshad code clean in interviews.
Validate n > 0 at the API boundary before digit work.
Save original before the digit loop zeroes n.
Belt-and-suspenders before original % sum.
State the radix unless the prompt specifies another base.
Harshad, not Harshad, and the single-digit edge case.
Pro Tip: dry-run 18 on paper (table above) before coding — it locks in the digit walk and the final %.
Mistakes that commonly break Harshad solutions in C.
For n = 0, digit sum is 0 and % 0 is undefined behavior.
→ Reject n ≤ 0 (and guard sum == 0) before dividing.
After the digit loop, n is 0 — you cannot test divisibility on it.
→ Keep original (or pass a copy into the digit-sum helper).
A while (n > 0) loop skips negative inputs entirely.
→ Reject negatives, or define and document absolute-value handling.
Harshad does not require reducing to a single digit.
→ Sum once, then test divisibility.
Other radices change both digits and the divisor.
→ Confirm base 10 unless the prompt says otherwise.
Check these inputs before calling the solution done.
s(1) = 1 and 1 % 1 == 0 — Harshad.
n = 0Not a positive Harshad number; reject before the modulus.
n < 0Out of the usual definition; reject or document abs handling.
n = 11s = 2 but 11 % 2 != 0.
Digit sums stay small; overflow is rare if you only add digits.
Clarify base 10 in APIs; other bases change both digits and the divisor.
Known results for common interview inputs.
n | s(n) | Harshad? |
|---|---|---|
1 | 1 | Yes |
12 | 3 | Yes |
11 | 2 | No |
18 | 9 | Yes |
Try these variations to lock in the pattern.
scanf and validate n > 0[1, 100]?is_harshadb1..9 are always Harshad because s(n) = n.original, accumulate sum, guard sum != 0 before %.Quick Takeaway: for positive n, sum decimal digits then test n % s == 0 — never divide by a zero digit sum.
| Task | Time | Extra space |
|---|---|---|
One n | O(log n) decimal digits | O(1) |
Scan [1, N] | O(N log N) digit work total | O(1) |
Here log n means base-10 logarithm: proportional to the number of decimal digits of n.
Harshad numbers are a small digit-sum exercise with clear interview payoff: extraction, divisibility, and careful zero guards in C. Master the single-check helper and the range scan so you can adapt either stop condition on the spot.
Practice the two examples above, then continue to LCM for another classic number-theory warm-up.
Keep original, sum digits, guard sum != 0, then test original % sum == 0 for positive n.
n > 0 and never divide by a zero digit sumn % 0 for zero or invalid inputsn then try to test divisibility on itn = 1 edge caseClassify them the interview-friendly way.
n % s(n) == 0
Definition% 10 then /= 10
CodeNever % 0
SafetyAlways Harshad
EdgeO(log n) digits
AnalysisThe same class of integers is often called Niven numbers in English-language sources (after Ivan Niven’s 1977 talk); Harshad comes from Sanskrit and means “joy-giver.”
Learn how to find the least common multiple using GCD in C.
8 people found this page helpful