Python Program to Print all Harshad Numbers within Given Range

In this article, we will learn how to print Harshad numbers within a specific range in Python. You will learn what a Harshad number is, how to check whether a given number is a Harshad number, and how to write a Python code that outputs all the Harshad numbers within the user-specified range.

Harshad Number:

If the given number is divisible by the sum of its constituent digits, we can conclude that the given number is a Harshad number.

Given the lower limit range and upper limit range, the task is to print all the Harshad numbers in the given range.

Examples:

Example1:

Input:

Given upper limit range =11
Given lower limit range =178

Output:

The Harshad numbers in the given range 11 and 178 are:
12 18 20 21 24 27 30 36 40 42 45 48 50 54 60 63 70 72 80 81 84 90 100 102 108 110 111 112 114 117 120 126
 132 133 135 140 144 150 152 153 156 162 171

Example2:

Input:

Given upper limit range =160
Given lower limit range =378

Output:

The Harshad numbers in the given range 160 and 378 are:
162 171 180 190 192 195 198 200 201 204 207 209 210 216 220 222 224 225 228 230 234 240 243 247 252 261
 264 266 270 280 285 288 300 306 308 312 315 320 322 324 330 333 336 342 351 360 364 370 372 375 378

Program to Print all Harshad Numbers within Given Range in Python

Below are the ways to print all the Harshad numbers in the given range.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Method #1: Using For Loop (Static Input)

Approach:

  • Create a function checkharshadnumb() which accepts the number as an argument and returns true if it is Harshad number else returns False.
  • Give the lower limit range as static input and store it in a variable.
  • Give the upper limit range as static input and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the for loop pass the iterator value to checkharshadnumb() function.
  • If it returns true then print the iterator value.
  • Inside the checkharshadnumb() function.
  • Make a copy of the number(argument) so you can verify the outcome later.
  • Make a result variable ( set to 0 ).
  • Create a while loop to go digit by digit through the number.
  • Every iteration, increase the result by a digit.
  • Divide the result by the number’s duplicate.
  • If a number divides perfectly, it is a Harshad number return True
  • Else return False.
  • The Exit of the Program.

Below is the implementation:

# Create a function checkharshadnumb() which accepts the number as an argument and
# returns true if it is Harshad number else returns False.


def checkharshadnumb(numb):
    # intiialize sum of digits to 0
    sum_of_digits = 0
    # copy the number in another variable(duplicate)
    dup_number = numb
    # Traverse the digits of number using for loop
    while dup_number > 0:
        sum_of_digits = sum_of_digits + dup_number % 10
        dup_number = dup_number // 10
    # If a number divides perfectly, it is a Harshad number return True
    # Else return False.
    if(numb % sum_of_digits == 0):
        return True
    else:
        return False


# Give the lower limit range as static input and store it in a variable.
lowlimrange = 11
# Give the upper limit range as static input and store it in another variable.
upplimrange = 178
print('The Harshad numbers in the given range',
      lowlimrange, 'and', upplimrange, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for m in range(lowlimrange, upplimrange+1):
        # Inside the for loop pass the iterator value to checkharshadnumb() function.
    if(checkharshadnumb(m)):
        # If it returns true then print the iterator value.
        print(m, end=' ')

Output:

The Harshad numbers in the given range 11 and 178 are:
12 18 20 21 24 27 30 36 40 42 45 48 50 54 60 63 70 72 80 81 84 90 100 102 108 110 111 112 114 117 120 126
 132 133 135 140 144 150 152 153 156 162 171

Method #2: Using For Loop (User Input)

Approach:

  • Create a function checkharshadnumb() which accepts the number as an argument and returns true if it is Harshad number else returns False.
  • Give the lower limit range and upper limit range as user input using map(),int(),split() functions.
  • Store them in two separate variables.
  • Inside the for loop pass the iterator value to checkharshadnumb() function.
  • If it returns true then print the iterator value.
  • Inside the checkharshadnumb() function.
  • Make a copy of the number(argument) so you can verify the outcome later.
  • Make a result variable ( set to 0 ).
  • Create a while loop to go digit by digit through the number.
  • Every iteration, increase the result by a digit.
  • Divide the result by the number’s duplicate.
  • If a number divides perfectly, it is a Harshad number return True
  • Else return False.
  • The Exit of the Program.

Below is the implementation:

# Create a function checkharshadnumb() which accepts the number as an argument and
# returns true if it is Harshad number else returns False.


def checkharshadnumb(numb):
    # intiialize sum of digits to 0
    sum_of_digits = 0
    # copy the number in another variable(duplicate)
    dup_number = numb
    # Traverse the digits of number using for loop
    while dup_number > 0:
        sum_of_digits = sum_of_digits + dup_number % 10
        dup_number = dup_number // 10
    # If a number divides perfectly, it is a Harshad number return True
    # Else return False.
    if(numb % sum_of_digits == 0):
        return True
    else:
        return False


# Give the lower limit range and upper limit range as
# user input using map(),int(),split() functions.
# Store them in two separate variables.
lowlimrange, upplimrange = map(int, input(
    'Enter lower limit range and upper limit range separate bt spaces = ').split())
print('The Harshad numbers in the given range',
      lowlimrange, 'and', upplimrange, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for m in range(lowlimrange, upplimrange+1):
        # Inside the for loop pass the iterator value to checkharshadnumb() function.
    if(checkharshadnumb(m)):
        # If it returns true then print the iterator value.
        print(m, end=' ')

Output:

Enter lower limit range and upper limit range separate bt spaces = 160 378
The Harshad numbers in the given range 160 and 378 are:
162 171 180 190 192 195 198 200 201 204 207 209 210 216 220 222 224 225 228 230 234 240 243 247 252 261
 264 266 270 280 285 288 300 306 308 312 315 320 322 324 330 333 336 342 351 360 364 370 372 375 378

Related Programs: