Print all Harshad Numbers in Python
Enter lower bound: 10
Enter upper bound: 49
10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48,
def main():
"""
Prints all the Harshad numbers within a given range.
"""
a = int(input("Enter lower bound: "))
b = int(input("Enter upper bound: "))
for i in range(a, b + 1):
if is_harshad(i):
print(i, end=", ")
print()
def is_harshad(number: int):
"""
Checks if a number is a Harshad number or not.
"""
total = sum_digits(number)
return total != 0 and number % total == 0
def sum_digits(number: int):
"""
Returns the sum of the digits of a number.
"""
BASE = 10
total = 0
n = abs(number)
while n != 0:
unit_digit = n % BASE
total += unit_digit
n //= BASE
return total
if __name__ == "__main__":
main()