Finding and Checking Armstrong Numbers with Easy Python Code

Advertisement

Apr 27, 2025 By Alison Perry

Numbers are more than just digits. Sometimes, they hide clever little tricks that feel almost magical once you spot them. Armstrong numbers are one of those surprises. You may have heard about them before, or maybe you're hearing the term for the first time — either way, once you understand how they work, you'll start noticing them more often. They are rare, interesting, and fun to figure out once you know the simple logic behind them. The good news is that checking for Armstrong numbers in Python is easier than you might think. You don't need to be a programming expert. Let's first break it down simply, and then I'll walk you through writing your own Python code to find these numbers!

What Exactly Is an Armstrong Number?

Here’s the simplest way to think about it: An Armstrong number is a number that’s equal to the sum of its own digits, each raised to the power of the number of digits.

Sounds like a mouthful, right? Let’s make it clear with a few examples:

  • 153 is an Armstrong number. Why?
  • 13+53+33=1+125+27=1531^3 + 5^3 + 3^3 = 1 + 125 + 27 = 15313+53+33=1+125+27=153
  • The number has three digits, so each digit gets raised to the third power.
  • 9474 is another Armstrong number.
  • 94+44+74+44=6561+256+2401+256=94749^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 947494+44+74+44=6561+256+2401+256=9474
  • Four digits? Each digit goes to the fourth power.

When the sum you get matches the original number, you’re looking at an Armstrong number.

They’re special because they don’t happen often, especially as numbers get bigger. It’s like finding a tiny rare gem hidden inside thousands of rocks.

How to Check for an Armstrong Number in Python

Now that you know what it means, let’s build a simple Python program that checks if a number is an Armstrong number. Don’t worry if you're not a pro coder; this is all beginner-friendly.

Here's the basic idea:

  1. Take the input number.
  2. Find the total number of digits.
  3. Split the number into digits.
  4. Raise each digit to the power of the number of digits.
  5. Add them all up.
  6. Compare the sum with the original number.

If they match, congratulations—you found an Armstrong number!

Here’s a simple Python code snippet to do that:

python

CopyEdit

# Python program to check if a number is an Armstrong number

# Taking input from the user

num = int(input("Enter a number: "))

# Find the number of digits

order = len(str(num))

# Initialize sum

sum = 0

# Make a copy of the original number

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** order

temp //= 10

# Display the result

if num == sum:

print(f"{num} is an Armstrong number.")

else:

print(f"{num} is not an Armstrong number.")

When you run this code, it will ask for a number and tell you if it’s an Armstrong number. Simple, clean, and easy to follow.

Finding Armstrong Numbers in a Range

Why stop at checking just one number? What if you want to find all the Armstrong numbers between 1 and 1000? Python makes that smooth, too.

The logic stays the same, but now you’ll loop through a range of numbers instead of checking just one. Here’s how you can do it:

python

CopyEdit

# Python program to find Armstrong numbers in a given range

# Define the range

lower = 1

upper = 1000

print(f"Armstrong numbers between {lower} and {upper} are:")

for num in range(lower, upper + 1):

order = len(str(num))

sum = 0

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** order

temp //= 10

if num == sum:

print(num)

This code will find and print all the Armstrong numbers from 1 to 1000. You’ll spot familiar ones like 153, 370, 371, and 407 showing up. It’s always fun to see the program spitting out these little mathematical oddities!

Making It Into a Python Function

If you’re coding something bigger and want to re-use the Armstrong number check multiple times, it’s a good idea to wrap the code inside a function. Functions are blocks of code that you can use again and again without rewriting the same lines.

Here’s how you can turn the Armstrong checker into a function:

python

CopyEdit

# Function to check if a number is an Armstrong number

def is_armstrong(number):

order = len(str(number))

sum = 0

temp = number

while temp > 0:

digit = temp % 10

sum += digit ** order

temp //= 10

return number == sum

# Example usage

n = int(input("Enter a number: "))

if is_armstrong(n):

print(f"{n} is an Armstrong number.")

else:

print(f"{n} is not an Armstrong number.")

Now, every time you want to check a number, you just call is_armstrong(number). Clean code means less chance of mistakes, and your program looks smarter, too.

And if you want to find Armstrong numbers in a range using this function, here’s a quick way:

python

CopyEdit

for i in range(1, 1001):

if is_armstrong(i):

print(i)

That’s the beauty of functions—they save time and make everything simpler.

Wrapping It Up

Armstrong numbers might seem like they're just a math trick, but they teach you a lot about how numbers work when you break them apart. Plus, coding them in Python is a solid exercise that uses loops, conditionals, and functions all in one go.

Whether you're a beginner practicing Python or someone brushing up your skills, writing a program to find Armstrong numbers is a fun little project that gives you instant feedback. Try tweaking the code, expanding the range, or even timing how fast Python finds the numbers. Once you see how neatly everything fits together, you'll realize coding isn't about memorizing; it's about understanding little puzzles like this one. Happy coding!

Advertisement

Recommended Updates

Technologies

Working with Exponents in Python: Everything You Need to Know

By Tessa Rodriguez / Apr 27, 2025

Learn different ways to handle exponents in Python using ** operator, built-in pow(), and math.pow(). Find out which method works best for your project and avoid common mistakes

Applications

7 New Canva Features That Make Creating Even Easier

By Tessa Rodriguez / Apr 28, 2025

Looking for ways to make designing easier and faster with Canva? Their latest updates bring smarter tools, quicker options, and fresh features that actually make a difference

Technologies

Master Full-Text Searching in SQL with the CONTAINS Function

By Alison Perry / Apr 27, 2025

Frustrated with slow and clumsy database searches? Learn how the SQL CONTAINS function finds the exact words, phrases, and patterns you need, faster and smarter

Technologies

Understanding the Differences Between ANN, CNN, and RNN Models

By Alison Perry / Apr 28, 2025

Understanding the strengths of ANN, CNN, and RNN can help you design smarter AI solutions. See how each neural network handles data in its own unique way

Applications

7 Must-Know Python Libraries for Effective Data Visualization

By Alison Perry / Apr 28, 2025

Which Python libraries make data visualization easier without overcomplicating things? This list breaks down 7 solid options that help you create clean, useful visuals with less hassle

Applications

7 University-Level Machine Learning Courses You Can Take for Free

By Tessa Rodriguez / Apr 28, 2025

Looking to learn machine learning without spending a dime? These 7 free university courses break things down simply and help you build real skills from scratch

Applications

Qwen2: Alibaba Cloud’s New Open-Source Language Model That’s Turning Heads

By Tessa Rodriguez / Apr 26, 2025

Discover how Alibaba Cloud's Qwen2 is changing the game in open-source AI. Learn what makes it unique, how it helps developers and businesses, and why it’s worth exploring

Technologies

Understanding HashMaps in Python for Faster Data Management

By Tessa Rodriguez / Apr 27, 2025

Ever wondered how Python makes data lookups so fast? Learn how HashMaps (dictionaries) work, and see how they simplify storing and managing information

Applications

Python Learning Made Easy with These YouTube Channels

By Alison Perry / Apr 28, 2025

Looking for Python tutorials that don’t waste your time? These 10 YouTube channels break things down clearly, so you can actually understand and start coding with confidence

Applications

How Kolmogorov-Arnold Networks Are Changing Neural Networks

By Tessa Rodriguez / Apr 27, 2025

Explore how Kolmogorov-Arnold Networks (KANs) offer a smarter, more flexible way to model complex functions, and how they differ from traditional neural networks

Applications

How to Track and Analyze IP Addresses Using Python

By Alison Perry / Apr 27, 2025

Learn how to track, fetch, and analyze IP addresses using Python. Find public IPs, get location details, and explore simple project ideas with socket, requests, and ipinfo libraries

Technologies

Mastering HLOOKUP in Excel: How to Find Data Across Rows Easily

By Tessa Rodriguez / Apr 26, 2025

Learn how to use HLOOKUP in Excel with simple examples. Find out when to use it, how to avoid common mistakes, and tips to make your formulas smarter and faster