Harshad Number or Niven Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main() {
    int n, sum=0, temp;
    scanf("%d", &n);
    temp = n;
    while(temp > 0) {
        sum += temp % 10;
        temp /= 10;
    }
    if(n % sum == 0) printf("%d is a Harshad Number", n);
    else printf("%d is not a Harshad Number", n);
    return 0;
}

C Output

Input:
18

Output:
18 is a Harshad Number



C++ Program

#include <iostream>
using namespace std;
int main() {
    int n, sum=0, temp;
    cin >> n;
    temp = n;
    while(temp > 0) {
        sum += temp % 10;
        temp /= 10;
    }
    if(n % sum == 0) cout << n << " is a Harshad Number";
    else cout << n << " is not a Harshad Number";
    return 0;
}

C++ Output

Input:
21

Output:
21 is not a Harshad Number



JAVA Program

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), sum = 0, temp = n;
        while(temp > 0) {
            sum += temp % 10;
            temp /= 10;
        }
        if(n % sum == 0)
            System.out.println(n + " is a Harshad Number");
        else
            System.out.println(n + " is not a Harshad Number");
    }
}

JAVA Output

Input:
12

Output:
12 is a Harshad Number



Python Program

n = int(input())
s = sum(int(d) for d in str(n))
if n % s == 0:
    print(n, "is a Harshad Number")
else:
    print(n, "is not a Harshad Number")

Python Output

Input:
25

Output:
25 is not a Harshad Number



In-Depth Learning – Entire Concept in Paragraphs
Example
A Harshad Number (or Niven Number) is a number that can be divided by the sum of its digits. For instance, 18 is a Harshad number since the sum of its digits is 1 + 8 = 9, and 18 ÷ 9 = 2 with no remainder. But 21 is not a Harshad number since the sum of the digits is 2 + 1 = 3, and 21 divided by 3 equals 7 (which is good). But take 25, and the sum is 2 + 5 = 7, and 25 divided by 7 has a remainder, so 25 is not Harshad.

Real-Life Analogy
Consider someone making money each month. If their overall salary divides cleanly into the same number of portions of each day they have worked, it is "fair" and balanced. Likewise, a Harshad number is fair and balanced since it can be divided by the digit sum of its parts. A number that cannot be divided evenly by its digit sum is like a salary that doesn't divide evenly into days—it has something extra or lacking.

Why It Matters
Harshad numbers are a great topic to study in number theory as they illustrate how numbers can have unique properties associated with their digits. This imparts us modular arithmetic (remainder, divisibility) practically. Knowledge of such properties enhances the problem-solving capability for competitive exams, coding interviews, and algorithm-based problems.

Learning Insights
Programming this program familiarizes starters with loops, modulus % operations, and digit manipulation. Dividing a number into digits through % 10 and / 10 is a programming basic skill that finds application in a large number of problems such as palindrome checking, Armstrong numbers, or digital root issues. Additionally, it presents conditional checking (if) with numeric reasoning.

Application in Interviews and Projects
In interviews, Harshad numbers can be presented as a challenge to your skill at working with the digits of a number. It's a popular one because the reasoning is straightforward, but it challenges clarity in conditionals and loops. Digit-sum checks turn out to be surprisingly ubiquitous in real-world projects—such as computing checksums, checking identification numbers, or building digit-based puzzles and games. For instance, some systems for digital security utilize digit sums as a fast integrity test prior to running more intense algorithms.

Extended Thought
Interestingly, all one-digit numbers are Harshad numbers, since every number is divisible by itself. Additionally, several higher numbers such as 18, 20, 21, 24, and 27 are Harshad. This characteristic connects with number divisibility rules, and students solving this problem attain a better insight into the nature of numbers.

SEO-Optimized Closing Paragraph
A Harshad number application in C, C++, Java, and Python is one of the most crucial beginner-level problems to learn digit-based computations and modular arithmetic. By practicing how to determine whether a number is divisible by the sum of digits, the students not only improve their logic in loops and conditionals, but also prepare themselves for interview coding rounds where number theory questions are frequently asked. No matter whether you are working on Harshad number assignments, preparing for competitive exams, or refreshing programming fundamentals, learning and implementing Harshad numbers will provide you with a sound understanding of mathematics-based programming principles.