Amstrong number

mail@pastecode.io avatar
unknown
plain_text
2 years ago
736 B
0
Indexable
n = int(input("Enter your number: "))
## try to test n = 12, 371, 153. 

# since we modify n in the loop, we cannot extract the input number again 
# we need a variable origin_n to store the original input number
# for comparison later
origin_n = n 
sum = 0 
while n != 0: 
    ## extract the last digit of the input number
    ## adjusting n to avoid infinite loop 
    ## also adjusting n so that in the next loop, we can extract the next last digit 
    remainder = n % 10 ## for example n = 123 --> then remainder = 3
    n = n // 10 ## for example: n = 123 --> then n = 12
    
    ## update sum 
    sum += remainder ** 3

if sum == origin_n:
    print(origin_n, "is an Amstrong number")
else: 
    print("not an Amstrong number")