To check if a number is an Armstrong number in Python, you can use the following code:
# function to check if number is Armstrong number or not
def isArmstrongNumber(num):
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# check if num is equal to the sum
return num == sum
# test for different numbers
numbers = [153, 370, 371, 407]
for num in numbers:
if isArmstrongNumber(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
In this code, the isArmstrongNumber() function takes a number as input and checks if it is an Armstrong number or not. It does this by calculating the sum of the cubes of each digit in the number, and checking if the sum is equal to the original number. If the sum is equal to the original number, the function returns True, indicating that the number is an Armstrong number. Otherwise, it returns False, indicating that the number is not an Armstrong number.
The code then tests the isArmstrongNumber() function for a few different numbers, and prints whether each number is an Armstrong number or not.
For example, when the code is run with the numbers 153, 370, 371, 407, it will output the following:
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is not an Armstrong number
This indicates that 153, 370, and 371 are Armstrong numbers, but 407 is not.