asked 166k views
4 votes
Write a python program to check whether a given number is Armstrong number or not. Note: If sum of digits raised to power number of digits as shown below is equal to the original digit then this number is called an Armstrong number. 1^4+6^4+3^4+4^4=1634 Sample outputs: Enter a number: 663 663 is not an Armstrong number Enter a number: 407 407 is an Armstrong number

1 Answer

0 votes

Here's a Python program to check whether a given number is an Armstrong number or not:

```python

def is_armstrong_number(num):

# Convert the number to string to count the number of digits

num_str = str(num)

num_digits = len(num_str)

# Calculate the sum of digits 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 == num:

return True

else:

return False

# Take input from the user

num = int(input("Enter a number: "))

# Check if the number is an Armstrong number

if is_armstrong_number(num):

print(num, "is an Armstrong number")

else:

print(num, "is not an Armstrong number")

```

In the above program, the function `is_armstrong_number` takes a number as input and checks whether it is an Armstrong number or not. It converts the number to a string to count the number of digits. Then, it calculates the sum of each digit raised to the power of the number of digits. Finally, it compares the sum with the original number and returns `True` or `False` accordingly.

The program prompts the user to enter a number, calls the `is_armstrong_number` function to check if it is an Armstrong number, and prints the appropriate message based on the result.

answered
User Andriy Kozachuk
by
7.2k points

No related questions found