Hello readers, welcome back to programminginpython.com, here in this post I am going to share with you another simple Python program that checks whether a number is an Armstrong number or not. So let’s code a program in python to check armstrong number.
Generally, a number is said to be an Armstrong number if an n -digit number equals the sum of the nth powers of its digits.
For Example, 153 is an Armstrong number as its sum of cubes of each digit 13 + 53 + 33 = 153 whereas 456 is not as its sum of cubes of each digit is not 456.
You can watch the video on YouTube here.
Armstrong number – Code Visualization
Task :
Python program to check an integer number is an Armstrong number or not.
Approach :
- Read an input number using
input()orraw_input(). - Check whether the value entered is an integer or not.
- Check input_num is greater than 0.
- Initialize a variable named
arm_numto 0. - Find
remainderof the input number by using mod (%) operator to get each digit in the number. - Now cube each digit and add it to
arm_num. - Floor Divide the number by 10.
- Repeat steps 5. 6. 7 until the input_num is not greater than 0.
- If
input_numis equal toarm_num, print number is ARMSTRONG. - When
input_numis not equals toarm_num, the number is NOT an Armstrong number.
Program :
__author__ = 'Avinash'
input_num = (input("Enter any number: "))
digit_len = len(str(input_num))
try:
arm_num = 0
val = int(input_num)
while val > 0:
reminder = val % 10
arm_num += reminder ** digit_len
val //= 10
if int(input_num) == arm_num:
print(input_num, 'is an ARMSTRONG number')
else:
print(input_num, 'is NOT an armstrong number')
except ValueError:
print("That's not a valid number, Try Again !")
Output :



Feel free to check some of the other posts here.
Similar Programs :
Course Suggestion
Want to learn python with strong fundamentals? If yes, I strongly suggest you to take the course below.
Course: Fundamentals of Programming in Python