Harshad Numbers

Find Harshad numbers (divisible by sum of digits).

Logic BuildingAdvanced
Logic Building
# Find Harshad numbers
print("Harshad numbers 1-100:")

for num in range(1, 101):
    digit_sum = sum(int(d) for d in str(num))
    if digit_sum > 0 and num % digit_sum == 0:
        print(num, end=" ")
print()

Output

Harshad numbers 1-100:
1 2 3 4 5 6 7 8 9 10 12 18 20 21 ...

Check if number divisible by digit sum.

Key Concepts:

  • Calculate sum of digits
  • Check if num % digit_sum == 0
  • Filter numbers