Program to Check if a Number is an Armstrong Number in Python
Armstrong Number
An Armstrong number (or narcissistic 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, 153 and 9474 are Armstrong numbers.
We will explore a method to check if a given number is an Armstrong number using Python programming.
Method: Using String Manipulation
We convert the number into a string, determine the number of digits, and compute the Armstrong condition.
def is_armstrong(num): num_str = str(num) num_digits = len(num_str) # Calculate the sum of digits raised to the power of num_digits sum_of_digits = sum(int(digit) ** num_digits for digit in num_str) return sum_of_digits == num # Get user input num = int(input("Enter a number: ")) if is_armstrong(num): print(f"{num} is an Armstrong number") else: print(f"{num} is not an Armstrong number")
Output:
Enter a number: 153 153 is an Armstrong number