Armstrong Numbers in Range

Find Armstrong numbers in range 1-1000.

Logic BuildingAdvanced
Logic Building
# Find Armstrong numbers
print("Armstrong numbers 1-1000:")

for num in range(1, 1001):
    # Count digits
    temp = num
    count = 0
    while temp > 0:
        count += 1
        temp //= 10
    
    # Calculate sum
    temp = num
    armstrong_sum = 0
    while temp > 0:
        digit = temp % 10
        armstrong_sum += digit ** count
        temp //= 10
    
    if armstrong_sum == num:
        print(num, end=" ")
print()

Output

Armstrong numbers 1-1000:
1 2 3 4 5 6 7 8 9 153 370 371 407

Check each number for Armstrong property.

Key Concepts:

  • Count digits
  • Calculate sum of digits^count
  • Compare with number