Finding Armstrong numbers using Python code is one of the popular exercises for Python interviews. It is asked to test your skills on number theory problems. Do you know what armstrong numbers are and how to find them using Python programs?
No problem, we will learn everything in this guide. It includes the definition and basic examples of Armstrong numbers, along with multiple Python programs that demonstrate various approaches to checking Armstrong numbers.
An Armstrong number is a special kind of number that is equal to the sum of its own digits, each raised to the power of the number of digits in that number. These are also known as narcissistic numbers or pluperfect digital invariant. Here is a mathematical representation of these numbers
Armstrong Number = d₁ⁿ + d₂ⁿ + d₃ⁿ + ... + dₙⁿ |
Here n is the total number of digits and d₁, d₂, ... are the individual digits.
A Simple Definition: Take every digit of a number, raise each to the power equal to the digit count, add them all up, if the result equals the original number, it's an Armstrong number.
Read Also: Python Exception Handling
Here are some of the common examples of Armstrong Numbers to help you how understand them perfectly:
| Number | Calculation |
|---|---|
| 153 (3-digit number) | 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 |
| 370 (3-digit number) | 3³ + 7³ + 0³ = 27 + 343 + 0 = 370 |
| 9474 (4-digit number) | 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474 |
We have different methods to check Armstrong numbers using Python programs, based on the concept. We will walk through each of them, so you will be ready to counter each question asked in interviews. Let’s begin the most basic approach!
This is the most basic method and great for beginners. It includes extracting digits using the modulus operator inside a while loop.
|
Output (input: 153):
|
Output (input: 123):
|
num % 10 extracts the last digitnum //= 10 removes the last digit after each iterationlen(str(num)) finds the total number of digitsn and accumulate the sum.Also Read: Python for Web Development
This method converts the number to a string to iterate over each digit cleanly. It is slightly more Pythonic than the while loop approach.
|
Output (input: 371):
|
String iteration over digits is clean and readable. The sum() combined with a generator expression makes this a compact, one-liner-style calculation.
It is a slightly more advanced way that can check Armstrong numbers multiple times in a program. You just have to give all the numbers with the program only, and it will test each number with the same logic.
|
Output:
|
Related Article: Python vs. R: What's the Difference?
This is an answer to one of the most asked Python interview questions, where they ask you to find out all the Armstrong numbers in a given range. Prepare this one, if you are going to attempt any interview soon.
|
Output:
|
Tip: This list comprehension approach is efficient and idiomatic Python. For very large ranges, consider adding early exit conditions or caching digit lengths.
Sometimes, you may be asked to not use loops. This is where you need to use the recursive approach. It is generally asked to check if the candidate have in-depth programming knowledge.
|
Output (input: 9474):
|
Note: This method is an efficient one and will give you the right answer each time, but Python has a default recursion limit of 1000 calls. You should mention this with your answer.
Also Read: Introduction to CherryPy: A Python Web Framework
Armstrong numbers are a classic problem asked in interviews and assessments to test your understanding of loops, modular arithmetic and string-to-integer conversion in Python. This guide has already covered multiple important methods you can use. You should understand each code and try it yourself for better understanding. This way you will be one step closer to becoming a Python developer.
Armstrong numbers are used as an educational tool to teach foundational programming logic to beginners. They help students master essential concepts like loop structures, conditional statements and dynamic digit extraction.
Armstrong numbers have no functional, real-world applications in commercial software development or data processing. They belong to recreational mathematics and are strictly used for academic training, coding interviews and hardware benchmarking.
Yes, all single-digit numbers (0–9) are Armstrong numbers by default. Because they contain only one digit, raising that single digit to the power of 1 always yields the original number itself.
The time complexity is O(log10N), where N represents the input number. This is because the total number of operations scales linearly with the number of digits, which is logarithmic to the value of N.