**Introduction**

In this article, we will explore the concept of Armstrong numbers and learn how to write a Python program to check whether a given number is an Armstrong number or not.

Also Read: Permute in Python: A Comprehensive Guide to Permutations

Armstrong numbers, also known as narcissistic numbers, are special numbers that are equal to the sum of their digits raised to the power of the number of digits in the number itself.

For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

## How to Check for an Armstrong Number

To check whether a number is an Armstrong number or not, we need to follow a few steps:

- Convert the number to a string to determine the number of digits.
- Calculate the sum of the digits raised to the power of the number of digits.
- Compare the calculated sum with the original number.
- If the calculated sum is equal to the original number, then the number is an Armstrong number.

Let’s dive into the Python program to implement this logic.

Also Read: 19 Pythonic Ways to Replace if-else Statements

**Python Program: Checking for an Armstrong Number**

```
def is_armstrong_number(number):
# Convert the number to a string
num_str = str(number)
# Calculate the number of digits
num_digits = len(num_str)
# Calculate the sum of the digits raised to the power of the number of digits
armstrong_sum = sum(int(digit)**num_digits for digit in num_str)
# Compare the calculated sum with the original number
if armstrong_sum == number:
return True
else:
return False
# Test the function
number = 153
if is_armstrong_number(number):
print(f"{number} is an Armstrong number")
else:
print(f"{number} is not an Armstrong number")
```

The above Python program defines a function `is_armstrong_number()`

that takes a number as an argument and checks whether it is an Armstrong number or not.

Also Read: Boost Python Code Efficiency: Eliminating Loops for Enhanced Performance

The program converts the number to a string, calculates the number of digits, and then calculates the sum of the digits raised to the power of the number of digits.

Finally, it compares the calculated sum with the original number and returns `True`

or `False`

accordingly.

**Python Program to Check Armstrong Number – FAQs**

**Q1: What is an Armstrong number?**

An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits in the number itself. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

**Q2: How do I check if a number is an Armstrong number in Python?**

You can check if a number is an Armstrong number in Python by converting the number to a string, calculating the number of digits, and then calculating the sum of the digits raised to the power of the number of digits. Finally, compare the calculated sum with the original number to determine if it is an Armstrong number or not.

**Q3: How can I optimize the Python program to check for Armstrong numbers?**

To optimize the Python program, you can avoid converting the number to a string by using mathematical operations to extract digits. Additionally, you can use a while loop instead of a for loop to calculate the sum of the digits raised to the power of the number of digits.

**Q4: Are there any other names for Armstrong numbers?**

Yes, Armstrong numbers are also known as narcissistic numbers or pluperfect digital invariants.

**Q5: Can you provide some examples of Armstrong numbers?**

Sure! Here are a few examples of Armstrong numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, and so on.

**Conclusion**

In this article, we learned about Armstrong numbers and how to write a Python program to check whether a given number is an Armstrong number or not.

Also Read: Python Program to Check If Two Strings are Anagram

We explored the concept of Armstrong numbers, discussed the steps involved in checking for an Armstrong number, and provided a Python program that implements the logic.

By using the program, you can easily verify if a number is an Armstrong number or not. So go ahead, try out the program, and have fun exploring the world of Armstrong numbers!

Also Read: Python Program to Delete an Element From a Dictionary