In the Python program, we will learn how to check whether the number is Armstrong number or not.
An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.
For Example, 153 is an Armstrong number.
(1)3 + (5)3 + (3)3= 153.
Here is the code of the program to check if the given number is Armstrong Number or Not.
# Python Program to Check whether the Number is Armstrong Number or Not
# Take input from the user
num = int(input("Enter a number: "))
# 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
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Enter a number: 5
5 is not an Armstrong number
Enter a number: 153
153 is an Armstrong number
# Python Program to Check whether the Number is Armstrong Number or Not
# Take input from the user
num = int(input("Enter a number: "))
# Changed num variable to string,
# and calculated the length (number of digits)
order = len(str(num))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Enter a number: 153
153 is an Armstrong number
Enter a number: 1634
1634 is an Armstrong number
Comments