3-digit Armstrong Number
Write a program to check if a given 3-digit number X is an Armstrong number or not.
Note: A number is an Armstrong number if the number is equal to the sum of the Nth power of its digits.
Input
The first line is an integer X.
Output
The output should be a single line containing True if it satisfies the given conditions, False in all other cases.
Explanation
In the given example X = 371,
The number of digits in 371 is 3.
So, 33 + 73 + 13 = 371. Hence, 371 is an Armstrong number.
So, the output should be True.
Sample Input 1
371
Sample Output 1
True
Sample Input 2
351
Sample Output 2
False
def IsArmstrong(n):
count = 0
digits = []
y = n
while n > 0:
i = n % 10
digits.append(i)
n //= 10
count += 1
sum = 0
for x in digits:
sum += x ** count
return (sum == y)
x = int(input())
print(IsArmstrong(x))
Comments
Leave a comment