Objective: Write a program in Python to check whether a given
number is Armstrong.
Code:
# Get user input
user_input = int(input("Enter a number to check if it's an Armstrong number: "))
# Convert the number to a string to find its length
num_str = str(user_input)
num_digits = len(num_str)
# Calculate the sum of each digit raised to the power of the number of digits
armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
# Check if the sum is equal to the original number
if armstrong_sum == user_input:
print(f"{user_input} is an Armstrong number.")
else:
print(f"{user_input} is not an Armstrong number.")
Output:
No comments:
Post a Comment