C Harshad Number Program

Beginner
5 min read
Updated: Oct 2026
2 programs
Live preview

What Is a Harshad Number?

A Harshad number (also called a Niven number) is a positive integer that is divisible by the sum of its digits.

Remember
n is Harshad  ⇔  n % digitSum(n) == 0
                (and digitSum(n) ≠ 0)

Example: 18
  digit sum = 1 + 8 = 9
  18 % 9 = 0  → Harshad

Example: 11
  digit sum = 1 + 1 = 2
  11 % 2 ≠ 0  → not Harshad
nDigit sumn % sumHarshad?
1890Yes
1230Yes
1121No
770Yes

How to Check a Harshad Number in C

Four clear steps — sum the digits, then test divisibility.

  1. Work with a positive value (reject n <= 0 for the standard definition).
  2. Compute the sum of decimal digits with % 10 and / 10.
  3. Guard against a digit sum of 0 (avoid modulus by zero).
  4. Return true when n % digitSum == 0.
MethodIdeaTimeBest for
Digit peeln % 10 / n / 10 sum, then modulusO(log n)Interviews, no strings
Char bufferFormat with sprintf, sum digit charsO(log n)Readable alternate style

Pseudocode

Pseudocode
function IsHarshad(n):
    if n <= 0:
        return false

    sum = 0
    temp = n
    while temp > 0:
        sum = sum + (temp mod 10)
        temp = temp / 10

    if sum == 0:
        return false

    return (n mod sum) == 0

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Definitionn % digitSum(n) == 0
Also calledNiven number
1–9All are Harshad
ZeroNot used in the standard positive definition
Digit sumPeel with % 10 and / 10
Classic check18 → yes; 11 → no

Live Preview

Enter a positive integer to see the digit sum and whether it is Harshad.

Positive whole numbers. Tap a chip or type a value.

Result
n = 18
Digit sum = 1 + 8 = 9
18 % 9 = 0
Verdict: HARSHAD NUMBER

Worked Walkthrough — n = 18

Sum the digits, then ask whether 18 divides evenly by that sum.

Calculation
temp = 18, sum = 0

18 % 10 = 8  → sum = 8,  temp = 1
1  % 10 = 1  → sum = 9,  temp = 0

digitSum = 9
18 % 9 = 0  ✓

Result: 18 is a Harshad number

C Programs

Start with the classic digit-peel check. Then use a char buffer when you want a more readable alternate.

Example 1 — Digit Peel

Sum digits with % 10 / / 10, then test n % sum == 0.

C
#include <stdio.h>
#include <stdbool.h>

bool is_harshad(int n)
{
    int sum = 0;
    int temp;

    if (n <= 0)
    {
        return false;
    }

    temp = n;
    while (temp > 0)
    {
        sum += temp % 10;
        temp /= 10;
    }

    return sum != 0 && n % 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;
}

How It Works

  1. Reject non-positive n for the standard definition.
  2. Copy n into temp so the original value stays available for the final modulus.
  3. Peel each digit with temp % 10 and shrink with temp /= 10.
  4. For 18: sum becomes 9, then 18 % 9 == 0.
  5. Require sum != 0 so you never modulus by zero.

Example 2 — Char Buffer

Format the number with sprintf, add each digit character, then apply the same divisibility test.

C
#include <stdio.h>
#include <stdbool.h>

bool is_harshad(int n)
{
    char buf[16];
    int sum = 0;
    int i;

    if (n <= 0)
    {
        return false;
    }

    sprintf(buf, "%d", n);
    for (i = 0; buf[i] != '\\0'; i++)
    {
        sum += buf[i] - '0';
    }

    return sum != 0 && n % sum == 0;
}

int main(void)
{
    printf("%s\\n", is_harshad(18) ? "true" : "false");
    printf("%s\\n", is_harshad(11) ? "true" : "false");
    return 0;
}

How It Works

  1. Reject n <= 0 the same way as Example 1.
  2. sprintf(buf, "%d", n) writes one character per digit.
  3. buf[i] - '0' converts a digit char into an integer 0–9.
  4. Sum those values, then check n % sum == 0.
  5. Same answers as digit peel — pick peel for interviews, char buffer when readability matters.

Edge Cases & Pitfalls

Check these before calling the solution done.

1–9

Always Harshad

Digit sum equals the number itself, so the remainder is always 0.

Zero

Avoid % 0

Digit sum of 0 is 0. Guard before the modulus.

Negatives

Usually false

Standard Harshad checks use positive integers only.

Temp copy

Keep original n

Destroy a copy while summing digits; modulus needs the original value.

Time and Space Complexity

ProgramTimeExtra space
Digit peelO(log n)O(1)
Char bufferO(log n)O(log n) for the buffer

Work is proportional to the number of digits. One modulus finishes the check.

Key Takeaways

  • Definition: n is divisible by the sum of its digits.
  • Steps: sum digits → guard sum ≠ 0 → n % sum == 0.
  • Alias: Harshad = Niven (base 10 on this page).
  • Test set: 18 yes; 12 yes; 11 no; 7 yes.

One line: sum the digits, then check whether that sum divides n.

Frequently Asked Questions

A positive integer n is Harshad (also called Niven) in base 10 when n is divisible by the sum of its decimal digits.
Yes. The digit sum of 1 is 1, and 1 % 1 == 0. Every one-digit positive integer is Harshad.
No. digitSum(11) = 2 and 11 % 2 != 0.
For n = 0 the digit sum is 0, and modulus by zero is invalid in C. The usual definition uses positive n.
Harshad uses one digit sum and a divisibility check. Condensing / digital root keeps summing until a single digit remains.

Next: LCM

Find the least common multiple of two numbers.

LCM tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful