Question 2
Write a Python program to check whether a given number is armstrong or not.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Python program to check if the number provided by the user is an Armstrong number or not # take input from the user nos = int(input("Enter a number: ")) # initialize sum sum = 0 # find the sum of the cube of each digit temp = nos while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 # display the result if nos == sum: print nos,"is an Armstrong number" else: print nos,"is not an Armstrong number" |
Explanation:
Armstrong number is the number whose sum of cube of individual digits is equal to the number itself.
Output:
1.
1 2 3 |
Enter a number: 371 371 is an Armstrong number |
1 2 3 |
Enter a number: 56 56 is not an Armstrong number |
Leave a Reply