Check Armstrong Number in Python

Beginner
⏱️ 12 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digits & powers

What you’ll learn

  • What an Armstrong number is, in simple school-level language.
  • How to count digits and build digit-power sums in Python.
  • One single-number checker and one range-printing program.
  • Edge cases, complexity, and interview-ready explanations.

Overview

If a number equals the sum of its digits raised to the power of total digits, it is Armstrong. Example: 153 = 1^3 + 5^3 + 3^3.

What is an Armstrong number?

For a number n with k digits, split digits and compute: d1^k + d2^k + ... + dk^k. If the result is n, then n is Armstrong.

The important idea: the exponent is the same for every digit, and it equals the number of digits.

Intuition and examples

153Armstrong
Sum
1^3 + 5^3 + 3^3 = 153
123Not Armstrong
Sum
1^3 + 2^3 + 3^3 = 36
7Armstrong
Reason
Single digit: 7^1 = 7

Live preview

Enter a value and see the digit-power sum and verdict.

Use integers n ≥ 1.

Live result
Press "Run check" to see details.

Algorithm

Goal: check if n is Armstrong.

Handle n <= 0

Return false for this tutorial.

Count digits

Find k = len(str(n)).

Sum powers

Add digit ** k for each digit.

Compare

If sum equals n, true; else false.

📜 Pseudocode

Pseudocode
function isArmstrong(n):
    if n <= 0:
        return false
    k = number of digits in n
    sum = 0
    for each digit d in n:
        sum = sum + d^k
    return sum == n
1

Check a single number

python
def is_armstrong(n: int) -> bool:
    if n <= 0:
        return False
    power = len(str(n))
    total = 0
    temp = n
    while temp > 0:
        digit = temp % 10
        total += digit ** power
        temp //= 10
    return total == n


number = 153
if is_armstrong(number):
    print(f"{number} is an Armstrong number.")
else:
    print(f"{number} is not an Armstrong number.")
2

Armstrong numbers in a range

python
def is_armstrong(n: int) -> bool:
    if n <= 0:
        return False
    power = len(str(n))
    total = 0
    temp = n
    while temp > 0:
        digit = temp % 10
        total += digit ** power
        temp //= 10
    return total == n


start, end = 1, 200
print(f"Armstrong numbers in the range {start} to {end}:")
for value in range(start, end + 1):
    if is_armstrong(value):
        print(value, end=" ")

❓ FAQ

A positive integer n is an Armstrong number if it equals the sum of its digits, where each digit is raised to the power of total digits in n. Example: 153 = 1^3 + 5^3 + 3^3.
Yes. For a one-digit number d, we have d^1 = d, so 1 to 9 are Armstrong numbers.
Some definitions include 0. In this tutorial, we check positive integers only, so n <= 0 returns false.
Integer power avoids rounding issues and keeps equality checks exact.
For one number, complexity is O(log n) because we process each digit a constant number of times.
Yes. Loop from low to high and test each value with the same helper function.

Edge cases and pitfalls

n <= 0

Return false

This tutorial uses positive integers.

Original n

Do not lose it

Use a temp variable while extracting digits.

Large values

Big powers

Power sums grow fast; use careful constraints.

⏱️ Time and space complexity

TaskTimeExtra space
Single checkO(log n)O(1)
Range 1..Uabout O(U log U)O(1)

Summary

  • Rule: digit-power sum equals the original number.
  • Code: count digits, sum powers, compare.
  • Practice: run on 153, 370, 371, 407 and nearby non-examples.
Did you know?

For 3-digit numbers, the Armstrong values are 153, 370, 371, and 407.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful