Print all Harshad numbers within given range in Python

Today, we will get to know how to print Harshad numbers within a given range in Python. If you are looking for a Python program to display Harshad numbers within the given range, you are at the right place. Here, you will get to know what is a Harshad number, a method to check whether a given number is Harshad number or not, and a Python program that prints all the Harshad numbers within the range given by the user.

Harshad numbers

If the given number is divisible by the sum of individual digits of that number, then we can say that the given number is a Harshad number. To understand the definition more clearly, let’s take an example-

Suppose, the number given as an input is 20.
Digits sum is = 2 + 0 = 2.
2 is a divisor of 20.
So, 20 is a Harshad number.

Now, let’s take another example.

Suppose, the number given as an input is 26.
Sum of digits is = 2 + 6 = 8.
But, 26 is not divisible by 8.
So, 26 is not a Harshad number.

 

Method for checking whether a number is a Harshad number in Python

So, to check the same, follow the steps accordingly-

  • Firstly, find the sum of individual digits-
  • Declare a variable ‘sum’ and initialize it with 0.
  • If the number is greater than 0, divide the number by 10 to obtain the remainder.
  • Store this remainder in the ‘digit’ variable.
  • Add the value in ‘digit’ to variable ‘sum’.
  • Now, divide the number by 10.
  • Finally, check whether the number is a Harshad number-
  • Check whether the number is divisible by the sum or not.
  • If the number is divisible, then the number is a Harshad number.

Python program to display Harshad numbers

So, let us see a program to print all Harshad numbers within the range given by the user. Here, we will take the lower and upper bound limits from the user as an input. The Python program is-

def check_harshad(number):
  remainder = 0
  digit_sum = 0
  check = False
  n = number
  while(n > 0):
    remainder = n % 10
    digit_sum = digit_sum + remainder
    n = n//10
  if number % digit_sum == 0:
    check = True
  return check

lower = int(input("ENTER LOWEST NUMBER : "))
upper = int(input("ENTER HIGHEST NUMBER : "))
print("HARSHAD NUMBERS WITHIN RANGE({},{}) ARE -".format(lower,upper))
for i in range(lower,upper+1):
  if check_harshad(i):
    print(i,end = " ")

In the above program, the function ‘check_harshad’ checks whether the number given is a Harshad number or not. If the number is a Harshad number, then it returns true. Using a ‘for’ loop, we print the numbers if the ‘check_harshad’ function returns true.

The output of the above program

So, the output of the above program is-

Lenovo-Y520-15IKBN:~/python$ python3 harshad.py
ENTER LOWEST NUMBER : 10
ENTER HIGHEST NUMBER : 30
HARSHAD NUMBERS WITHIN RANGE(10,30) ARE -
10 12 18 20 21 24 27 30 
[email protected]:~/python$

In the sample execution of the Python program, the lower and upper bound entered by the user is 10 and 30. So, within this range, there are 8 Harshad numbers- 10, 12, 18, 20, 21, 24, 27, 30.

Thanks for reading this tutorial. I hope it helps you.

Also read: Check if a given number is Fibonacci number in Python

Leave a Reply

Your email address will not be published. Required fields are marked *