Implementing Harshad number in Python

Filed Under: Python
Harshad Number

Hello, readers. This article focuses on Implementing the Harshad number in Python with scenarios and examples.

So, let us begin! 馃檪

Understanding the Harshad Number

Number System, as we all, is the base of every mathematical model in this universe. When it comes to programming, we do deal with numbers and a way to manipulate the data variables through in-built modules and functions.

One such concept of number is a Harshad Number.

Harshad Number is an integer that is divisible by the sum of its digits when written in that base.

So, at first, we need to accept the data value as input. Further, we need to segregate the original number to get hold of all the digits that make up the number individually. Then, we perform a summation of all the digits that collectively make up the actual number.

The final step is to divide the original number by the summation results. If the result is 0, it states that the number is completely divisible by the summation score. Thus, we can state the original number as a Harshad number.

If we get any non-zero value as the output of the modulus operation, we conclude that the original number is not a Harshad number.

Scenarios for Harshad Number

Now, let us try implementing the above pseudo logic on actual numbers.


Input number: 156

Digits of the number: 1, 5 and 6

Summation of digits: 1 + 5 + 6 = 12

Modulus operation: 156%12 = 0

As the result of the mod operation is zero, we state that 156 is a Harshad number.

Generic Algorithm of Harshad Number

To check for a Harshad Number, we need to follow the below algorithm.

  1. Accept the number as input.
  2. We usually store a copy of this input value separately for comparision later in the process.
  3. We then traverse through the number using a while or for loop to get the entire number in a separate digit form.
  4. While traversing, we keep adding the digits to get the summation of all the digits.
  5. Repeat the step 3 and 4 until all the digits are exhausted.
  6. Divide the original number by the summation results
  7. If the number is completely divisible and returns zero as the remainder score, then we conclude the number to be a Harshad number.

Implementing Harshad number in Python

Have a look at the below code.


num = input()
num =int(num)
ans = 0
    dig = num%10
    ans=ans + dig
if(num_n % ans == 0):
    print("The entered num is a Harshad Number!")
    print("It is not a Harshad Number!")


  1. At first, we accept the number from the user input() function.
  2. We store the original’s number copy into an another variable for comparison at the end.
  3. We traverse through the digits of the number using the while loop. Using the mod operator, we try to extract a digit at a single instance of loop from the number and then perform summation of the digits to store the result of the summation in the variable ans.
  4. At last, we perform mod operation on the original number and the summation score. If it results into 0, we conclude that number as a Harshad number.


The entered num is a Harshad Number! 

The entered num is a Harshad Number!

It is not a Harshad Number!


By this, we have come to the end of this topic. Feel free to comment below, in case you have any questions.

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning! 馃檪

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors