C++ Program to Check Harshad Number

C++ Program to Check Harshad Number

Harshad numbers, also called Niven numbers, are numbers that are divisible by the sum of their digits. For example, 18 is a Harshad number because 1 + 8 = 9, and 18 is divisible by 9. Learning to check Harshad numbers in C++ is a fun way for beginners to practice loops, modular arithmetic, and conditional statements while understanding a basic mathematical concept.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Basic Method to Check Harshad Number

This program calculates the sum of digits of a number using a simple loop and then checks if the original number is divisible by that sum.

#include <iostream>

using namespace std;

int main() {

    int n, temp, sum = 0, digit;

    cout << "Enter a number: " << endl;
    cin >> n;

    temp = n;

    while(temp > 0) {

        digit = temp % 10;
        sum += digit;
        temp /= 10;

    }

    if(n % sum == 0)
        cout << n << " is a Harshad number." << endl;

    else
        cout << n << " is not a Harshad number." << endl;

    return 0;

}

In this program, we extract each digit using the modulus operator and add it to a sum. Checking divisibility of the number by this sum determines if it is a Harshad number. Beginners can easily understand how loops and modulus operations are used for digit-based calculations.

Program 2: Using a Function for Reusability

Encapsulating the logic in a function makes the program modular and reusable. This method is handy when you want to check multiple numbers in a single program.

#include <iostream>

using namespace std;

bool isHarshad(int n) {

    int temp = n, sum = 0;

    while(temp > 0) {

        sum += temp % 10;
        temp /= 10;

    }

    return n % sum == 0;

}

int main() {

    int n;

    cout << "Enter a number: " << endl;
    cin >> n;

    if(isHarshad(n))
        cout << n << " is a Harshad number." << endl;

    else
        cout << n << " is not a Harshad number." << endl;

    return 0;

}

By using the isHarshad() function, we separate the core logic from input/output operations. Beginners can see the benefits of modular programming, as functions make code cleaner, easier to debug, and reusable across different programs.

Program 3: Optimized Method Using a Digit Sum Function

For efficiency, especially when checking many numbers, you can create a separate function to calculate the sum of digits and reuse it to check divisibility. This reduces repeated code and improves readability.

#include <iostream>

using namespace std;

int sumOfDigits(int n) {

    int sum = 0;

    while(n > 0) {

        sum += n % 10;
        n /= 10;

    }

    return sum;

}

bool isHarshadOptimized(int n) {

    int sum = sumOfDigits(n);
    return n % sum == 0;

}

int main() {

    int n;

    cout << "Enter a number: " << endl;
    cin >> n;

    if(isHarshadOptimized(n))
        cout << n << " is a Harshad number." << endl;

    else
        cout << n << " is not a Harshad number." << endl;

    return 0;

}

This method demonstrates how creating small helper functions improves program structure. Beginners can understand that breaking problems into smaller parts makes programs more manageable and easier to extend or debug.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask about Harshad numbers in C++:

Q1: What is a Harshad number?
A Harshad number is divisible by the sum of its digits. For example, 18 is a Harshad number because 18 ÷ (1 + 8) = 2.

Q2: Can a single-digit number be Harshad?
Yes, all single-digit numbers from 1 to 9 are considered Harshad numbers.

Q3: Why use a function to check Harshad numbers?
Functions make the code modular, reusable, and easier to read, especially when checking multiple numbers.

Q4: Can I use the sum of digits function for other programs?
Yes, the sumOfDigits() function can be reused in programs involving digit calculations, divisibility checks, or numeric patterns.

Q5: Where are Harshad numbers used?
Harshad numbers are mainly used as mathematical exercises to practice number operations, loops, and modular arithmetic.

Conclusion

Checking Harshad numbers in C++ is a great exercise for beginners to practice loops, functions, and modular arithmetic. We explored three methods: a basic loop method, a reusable function approach, and an optimized method using a helper function. Practicing these programs helps beginners strengthen their understanding of number operations and modular code design while improving problem-solving skills.

Additional & References

Learning about Harshad numbers introduces beginners to number properties and modular programming. Try generating sequences of Harshad numbers or combining the concept with other number-based exercises to enhance learning.

Scroll to Top