Harshad Numbers

Find Harshad numbers (divisible by sum of digits).

AdvancedPhase 6 - Category 1: Number LogicExample 10 of 10
harshad-numbers.py
1# Find Harshad numbers
2print("Harshad numbers 1-100:")
3
4for num in range(1, 101):
5 digit_sum = sum(int(d) for d in str(num))
6 if digit_sum > 0 and num % digit_sum == 0:
7 print(num, end=" ")
8print()

Output

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

What's going on

Check if number divisible by digit sum.

Key Concepts:

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