Zed-King Institute

    Armstrong Number in Python

    mediumwhile loopPython

    A number is an Armstrong number when the sum of its digits, each raised to the power of the number of digits, equals the number itself.

    Quick answer

    An Armstrong number is equal to the sum of its digits, each raised to the power of the number of digits: 153 = 1³ + 5³ + 3³. In Python, count the digits with k = len(str(n)), add digit ** k for every digit, and compare the total with n. The 3-digit Armstrong numbers are 153, 370, 371 and 407.

    The question

    Read a positive integer and print whether it is an Armstrong number: the sum of each digit raised to the power of the number of digits equals the number itself (153 = 1³ + 5³ + 3³).

    The code

    program.py

    n = int(input())
    k = len(str(n))
    total = 0
    temp = n
    while temp > 0:
        total += (temp % 10) ** k
        temp //= 10
    if total == n:
        print(n, "is an Armstrong number")
    else:
        print(n, "is not an Armstrong number")

    Input

    123

    Output

    123 is not an Armstrong number

    How it works

    1. 1k = len(str(n)) counts the digits — 3 for 153, 4 for 1634.
    2. 2Work on a copy (temp) so n is still available for the final comparison.
    3. 3Add (temp % 10) ** k for every digit, removing digits with temp //= 10.
    4. 4Compare the total with n.

    Dry run: is 153 an Armstrong number?

    153 has 3 digits, so k = 3. Each pass of the while loop takes the last digit, raises it to the power 3 and adds it to total.

    temptemp % 10digit ** 3totaltemp // 10
    1533272715
    1551251521
    1111530

    temp is now 0, so the loop stops. total = 153 = n, so 153 is an Armstrong number.

    Common mistakes

    • Always cubing (** 3) — it works for 153 but fails for 4-digit Armstrong numbers like 1634.
    • Changing n itself in the loop, so the final comparison is against 0.

    Armstrong number in Python using a for loop

    Turn the number into a string and the for loop hands you one digit at a time — no % or // needed. int(d) changes each character back into a number.

    program.py

    n = int(input())
    digits = str(n)
    k = len(digits)
    total = 0
    for d in digits:
        total += int(d) ** k
    if total == n:
        print(n, "is an Armstrong number")
    else:
        print(n, "is not an Armstrong number")

    Input

    153

    Output

    153 is an Armstrong number

    Print all Armstrong numbers between 100 and 999

    The same digit loop, placed inside a for loop that tries every 3-digit number. Every number here has three digits, so the power is fixed at 3.

    program.py

    for n in range(100, 1000):
        total = 0
        temp = n
        while temp > 0:
            total += (temp % 10) ** 3
            temp //= 10
        if total == n:
            print(n)

    Input

    (no input)

    Output

    153
    370
    371
    407

    Armstrong number in Python using a function

    sum() with a generator does the whole digit loop in one line. The function returns True or False, so it can be reused — for example to test every number in a range.

    program.py

    def is_armstrong(n):
        s = str(n)
        return n == sum(int(d) ** len(s) for d in s)
    
    n = int(input())
    if is_armstrong(n):
        print(n, "is an Armstrong number")
    else:
        print(n, "is not an Armstrong number")

    Input

    9474

    Output

    9474 is an Armstrong number

    Questions students ask

    What is an Armstrong number in Python?
    It is a number that equals the sum of its own digits, each raised to the power of the number of digits. 153 has three digits and 1³ + 5³ + 3³ = 1 + 125 + 27 = 153, so 153 is an Armstrong number. In Python you check it by taking the digits out one by one with % 10 and // 10.
    Which numbers are Armstrong numbers?
    The 3-digit Armstrong numbers are 153, 370, 371 and 407. The 4-digit ones are 1634, 8208 and 9474. Every single-digit number is also an Armstrong number, because a digit raised to the power 1 is itself.
    What do 153 % 10 and 153 // 10 give in Python?
    153 % 10 gives 3, the last digit. 153 // 10 gives 15, the number with its last digit removed. Repeating the two operations walks through every digit: 3, then 5, then 1.
    Is 1634 an Armstrong number?
    Yes. It has four digits, so each digit is raised to the power 4: 1 + 1296 + 81 + 256 = 1634. A program that always cubes the digits gets this one wrong.
    Is an Armstrong number the same as a narcissistic number?
    Yes. Narcissistic number and pluperfect digital invariant are other names for the same idea: a number equal to the sum of its own digits, each raised to the power of the number of digits.

    Now solve a similar question yourself

    A new Python question every time, checked instantly.

    Practice questions

    Preparing for NIELIT O Level?

    This program is the kind asked in the M3 (Python) theory paper and the PR-3 practical exam.

    Want to build websites and apps with AI tools instead of writing every line yourself? See our AI Digital Pro course in Kaithal.

    More in while loop