No products in the cart.
Return To ShopPython
Python Program to Check Armstrong Numbers
Armstrong numbers, also known as narcissistic numbers, are fascinating mathematical curiosities. In this article, we will explore what Armstrong numbers are and provide a detailed, step-by-step guide on how to write a Python program to check Armstrong numbers.
What are Armstrong Numbers?
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. For example, the number 153 is an Armstrong number because:
153 = 13 + 53 + 33 = 1 + 125 + 27 = 153
In general, a number 𝑛 with 𝑑 digits is an Armstrong number if:
n = ∑𝑖=1d (𝑑𝑖𝑔𝑖𝑡𝑖)d
where 𝑑𝑖𝑔𝑖𝑡𝑖 is the 𝑖-th digit of 𝑛.
Steps to Write a Python Program to Check Armstrong Numbers
To write a Python program to check if a given number is an Armstrong number, follow these steps:
- Input the Number: Accept the number from the user.
- Determine the Number of Digits: Calculate the number of digits in the number.
- Calculate the Sum of Powers: Compute the sum of each digit raised to the power of the number of digits.
- Compare and Output the Result: Compare the calculated sum with the original number to determine if it is an Armstrong number.
Step-by-Step Implementation in Python
Step 1: Input the Number
First, we need to get the number from the user. We can use the input()
function to achieve this:
number = int(input("Enter a number: "))
Step 2: Determine the Number of Digits
Next, we determine the number of digits in the number. We can convert the number to a string and use the len()
function:
num_str = str(number) num_digits = len(num_str)
Step 3: Calculate the Sum of Powers
We need to iterate through each digit, raise it to the power of num_digits
, and sum these values. We can use a for
loop for this:
sum_of_powers = 0 for digit in num_str: sum_of_powers += int(digit) ** num_digits
Step 4: Compare and Output the Result
Finally, we compare sum_of_powers
with the original number and print the result:
if sum_of_powers == number: print(f"{number} is an Armstrong number.") else: print(f"{number} is not an Armstrong number.")
Full Python Program
Combining all the steps, the complete Python program looks like this:
# Program to check if a number is an Armstrong number # Input the number from the user number = int(input("Enter a number: ")) # Convert the number to a string to determine the number of digits num_str = str(number) num_digits = len(num_str) # Initialize the sum of powers sum_of_powers = 0 # Calculate the sum of each digit raised to the power of num_digits for digit in num_str: sum_of_powers += int(digit) ** num_digits # Check if the calculated sum is equal to the original number if sum_of_powers == number: print(f"{number} is an Armstrong number.") else: print(f"{number} is not an Armstrong number.")
Conclusion
In this article, we have explored what Armstrong numbers are and provided a comprehensive guide to writing a Python program to check for Armstrong numbers. By following the steps outlined, you can understand and implement the concept of Armstrong numbers in Python effectively. This knowledge can be a valuable addition to your programming toolkit, especially for understanding numerical properties and enhancing your coding skills.
For more articles on Python programming and other tech-related topics, Keep Visiting Winorbit Learning. Happy coding!