armstrong number in python

Demystifying Armstrong Number in Python: A Pythonic Exploration

Getting your Trinity Audio player ready...

Summary: An Armstrong number, also known as a narcissistic number, is a special type of number in mathematics and computer science. In Python, identifying Armstrong numbers involves calculating the sum of each digit raised to the power of the total number of digits in the number. If the sum equals the original number, it is considered an Armstrong number. Utilizing simple functions and loops, Python programmers can efficiently identify Armstrong numbers within a given range. This concept offers insights into number theory and serves as a practical exercise in programming logic and problem-solving skills. Understanding Armstrong numbers enriches one’s grasp of mathematical concepts and programming techniques.

Demystifying Armstrong Number in Python: A Pythonic Exploration

Have you ever encountered a number that seems to hold a hidden secret within its digits? Enter Armstrong numbers, intriguing mathematical entities with a fascinating property. Armstrong number is named after the mathematician S. D.Armstrong.

What makes them so captivating? Well, the sum of the cubes of their individual digits, when added together, magically yields the original number itself! Take the number 153, for instance. 1 cubed is 1, 5 cubed is 125, and 3 cubed is 27. Add these up, and voilà – you get 153! This fascinating property sparks our curiosity and invites us to delve deeper into the world of Armstrong numbers.

In this blog, we’ll delve into the world of Armstrong numbers and explore how to identify them using the powerful Python programming language.

Unveiling The Mystery

An Armstrong number in python is a positive integer where the sum of the cubes of its individual digits is equal to the original number itself. Let’s consider the number 153:

  • 1 cubed is 1
  • 5 cubed is 125
  • 3 cubed is 27

Adding these cubed values, we get: 1 + 125 + 27 = 153

Since the sum of the cubes equals the original number, 153 confirms itself as an Armstrong number!

Python Takes The Stage

Now, let’s translate this mathematical concept into Python code. Here’s a function to check if a given number is an Armstrong number:


def is_armstrong_number(number):


  This function checks if a given number is an Armstrong number.



  number: The integer to be evaluated.



  True if the number is an Armstrong number, False otherwise.


  original_number = number

  sum_of_cubes = 0

 while number > 0:

    # Extract the last digit

digit = number % 10

    # Cube the digit and add it to the sum

sum_of_cubes += digit ** 3

    # Remove the last digit from the number

number //= 10


 return sum_of_cubes == original_number

Explanation Breakdown

  1. Function definition: We define a function named is_armstrong_number that takes an integer (number) as input.
  2. Initialization: We store the original number and initialize a variable sum_of_cubes to hold the cumulative sum of cubed digits.
  3. Looping and calculation: A while loop iterates as long as the number is greater than 0. In each iteration:
    • The last digit is extracted using the modulo operator (%).
    • The extracted digit is cubed using the exponentiation operator (**) and added to the sum_of_cubes.
    • The last digit is removed from the number using integer division (//).
  4. Comparison and return: After the loop, we compare the sum_of_cubes with the original number. The function returns True if they are equal, indicating an Armstrong number; otherwise, it returns False.

Putting it to The Test

Now, let’s use this function to check if 153 is

indeed an Armstrong number:

# Python program to determine whether

# the number is Armstrong number or not

# Function to calculate x raised to

# the power y

def power(x, y):         

         if y == 0:

                     return 1

         if y % 2 == 0:

                     return power(x, y // 2) * power(x, y // 2)     

         return x * power(x, y // 2) * power(x, y // 2)

# Function to calculate order of the number

def order(x):

         # Variable to store of the number

         n = 0

         while (x != 0):

                     n = n + 1

                     x = x // 10

         return n

# Function to check whether the given

# number is Armstrong number or not

def isArmstrong(x):

         n = order(x)

         temp = x

         sum1 = 0

         while (temp != 0):

                     r = temp % 10

                     sum1 = sum1 + power(r, n)

                     temp = temp // 10

         # If condition satisfies

         return (sum1 == x)

# Driver code

x = 153


x = 1253


Beyond the basics

The provided code is a basic implementation for identifying Armstrong numbers. You can further enhance it by:

  • Adding error handling: Check for invalid inputs like negative numbers.
  • Creating a function to find all Armstrong numbers within a specific range.
  • Implementing unit tests to ensure the function’s correctness.

By exploring these extensions, you can gain a deeper understanding of Armstrong numbers and refine your Python skills in identifying them efficiently. 

Embrace The Journey!

The world of programming opens doors to fascinating explorations like unravelling the secrets of Armstrong numbers. With dedication and practice, you can continue your Pythonic journey, delving into even more intricate concepts and building captivating applications.

Start Learning Python Today with Pickl.AI

Make the right start today with Pickl.AI. Our Python programming course has been meticulously designed to provide comprehensive knowledge of Python and its application. To explore more on how can start your learning journey, connect with Pickl.AI.


Frequently Asked Questions

Armstrong Numbers hold significance in mathematics and computer science due to their unique properties and applications.

You can write a Python function to calculate the sum of digits raised to a power and compare it with the original number.

Yes, Armstrong Numbers find applications in cryptography for generating secure keys and hashes.

No, Armstrong Numbers are defined only for non-negative integers.

Armstrong Numbers exist for any number of digits; however, the computational complexity increases for larger numbers.

Yes, you can extend the range for finding Armstrong Numbers by optimizing your code and leveraging efficient algorithms.


  • Shriya Singh

    Written by:

    I often try bringing verities to the world by stitching my soul into the fabric of words. Making it to the ground, I try to discover the intricate folds of life while sipping coffee.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments