Arunav Goswami
Data Science Consultant at almaBetter
Learn about Armstrong numbers in Python with for loop, while loop, and function examples. Explore how to code Armstrong numbers effectively in this lesson
An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its digits raised to the power of the number of digits. This article will explore the concept of Armstrong numbers, how to identify them, and provide various Python implementations using loops and functions.
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number because:
1³ + 5³ + 3³ = 1 + 125 + 27 = 153
This concept can be generalized for any number of digits. For an n-digit number, the number is considered an Armstrong number if the sum of its digits each raised to the power of n equals the original number.
Armstrong numbers can exist for any number of digits. Here are some examples of n-digit Armstrong numbers:
153: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153
370: 3³ + 7³ + 0³ = 27 + 343 + 0 = 370
371: 3³ + 7³ + 1³ = 27 + 343 + 1 = 371
407: 4³ + 0³ + 7³ = 64 + 0 + 343 = 407
1634: 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1 + 1296 + 81 + 256 = 1634
8208: 8⁴ + 2⁴ + 0⁴ + 8⁴ = 4096 + 16 + 0 + 4096 = 8208
9474: 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474
54748: 5⁵ + 4⁵ + 7⁵ + 4⁵ + 8⁵ = 3125 + 1024 + 16807 + 1024 + 32768 = 54748
92727: 9⁵ + 2⁵ + 7⁵ + 2⁵ + 7⁵ = 59049 + 32 + 16807 + 32 + 16807 = 92727
93084: 9⁵ + 3⁵ + 0⁵ + 8⁵ + 4⁵ = 59049 + 243 + 0 + 32768 + 1024 = 93084
548834: 5⁶ + 4⁶ + 8⁶ + 8⁶ + 3⁶ + 4⁶ = 15625 + 4096 + 262144 + 262144 + 729 + 4096 = 548834
These examples illustrate how Armstrong numbers exist for different digit lengths, with each number matching the sum of its digits raised to the appropriate power.
To find Armstrong numbers in Python, the primary steps are:
Here, we will implement different methods to check Armstrong numbers using loops and functions.
This approach uses a for loop to iterate through the digits of a number and calculate whether it is an Armstrong number.
# Python program to check Armstrong number using for loop
# Input number
num = int(input("Enter a number: "))
# Convert number to string to easily iterate through each digit
num_str = str(num)
# Calculate the length of the number (number of digits)
n = len(num_str)
# Initialize sum
armstrong_sum = 0
# Loop through each digit
for digit in num_str:
# Add the nth power of each digit to the sum
armstrong_sum += int(digit) ** n
# Check if the calculated sum matches the original number
if armstrong_sum == num:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
Explanation:
A while loop can also be used to perform the same operation. This approach demonstrates a different way of extracting digits.
# Armstrong number program in python using while loop
# Input number
num = int(input("Enter a number: "))
# Make a copy of the number for comparison later
temp = num
# Initialize sum
armstrong_sum = 0
# Find the number of digits
n = len(str(num))
# Loop until all digits are processed
while temp > 0:
# Get the last digit
digit = temp % 10
# Add the nth power of the digit to the sum
armstrong_sum += digit ** n
# Remove the last digit from temp
temp //= 10
# Check if the calculated sum matches the original number
if armstrong_sum == num:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
Explanation:
Creating a function in Python makes the code reusable and more organized.
# Function to check if a number is an Armstrong number
def is_armstrong(num):
# Convert number to string to calculate the number of digits
n = len(str(num))
# Initialize sum
armstrong_sum = 0
# Temporary variable to manipulate the number
temp = num
while temp > 0:
digit = temp % 10
armstrong_sum += digit ** n
temp //= 10
# Return True if sum equals the original number
return armstrong_sum == num
# Input number
number = int(input("Enter a number: "))
# Check and display result
if is_armstrong(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")
Explanation:
Often, the task is to find all Armstrong numbers within a specified range. Below is a code snippet demonstrating this.
# Python Program for Armstrong Number within a range
# Input range
lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))
# Loop through the range
for num in range(lower, upper + 1):
# Calculate the number of digits
n = len(str(num))
# Initialize sum
armstrong_sum = 0
# Copy of the number
temp = num
while temp > 0:
digit = temp % 10
armstrong_sum += digit ** n
temp //= 10
# Print the number if it is an Armstrong number
if num == armstrong_sum:
print(num)
Explanation:
Boost your coding skills with our online Python compiler, explore in-depth Python tutorials, and get our handy Python cheat sheet!
Armstrong numbers, though primarily a mathematical curiosity, have several educational and computational applications:
Cryptography and Digital Security:
Number Theory Research:
Game Development and Puzzle Design:
Benchmarking and Testing:
Digital Art and Recreational Mathematics:
These applications highlight Armstrong numbers as useful in educational and computational contexts, offering valuable insights into number theory and programming.
Armstrong numbers are an interesting concept in number theory and can be easily implemented in Python using different approaches. Whether using loops or functions, checking if a number is an Armstrong number involves straightforward calculations of powers and sums. Mastering this concept provides a deeper understanding of Python’s numerical operations and control structures.
Enroll in our Data Science course today and take the first step toward earning your Masters in Data Science!
Related Articles
Top Tutorials