Python Program to Check Armstrong Number

In Python programming, an Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. Checking for Armstrong numbers is an interesting exercise that involves manipulating numbers and performing calculations. In this blog post, we will explore how to write a Python program to check for Armstrong numbers, understand the logic behind it, and provide examples to demonstrate its implementation.

What is an armstrong number in python

An Armstrong number in Python is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because: 13+53+33=15313+53+33=153. Armstrong numbers are also known as narcissistic, pluperfect, or pluperfect digital invariant numbers. They are interesting mathematical curiosities and can be identified using code to check if a given number meets this criteria.

Check for Armstrong Numbers in Python

To check if a given number is an Armstrong number, we need to extract its individual digits, raise each digit to the power of the number of digits, and sum up the results. If the sum is equal to the original number, it is an Armstrong number.

Example: Python program to check Armstrong logic:

def is_armstrong_number(number):
    num_str = str(number)
    num_digits = len(num_str)
    sum = 0

    for digit in num_str:
        sum += int(digit) ** num_digits

    return sum == number

number = 153
if is_armstrong_number(number):
    print(number, "is an Armstrong number")
else:
    print(number, "is not an Armstrong number")

Output:

153 is an Armstrong number

Conclusion

Checking for Armstrong numbers in Python involves extracting digits, performing calculations, and verifying the result against the original number. In this blog post, we explored the logic behind checking for Armstrong numbers and provided an example Python program to demonstrate its implementation. You can experiment with different numbers and further optimize the program for efficiency. Checking for Armstrong numbers is a fun exercise that helps you enhance your number manipulation skills in Python. Happy coding!