Python Program - Check Armstrong Number
sptth://www.theitroad.com
here's a Python program that checks if a given number is an Armstrong number:
# function to check if a number is an Armstrong number def is_armstrong(number): # convert the number to a string to get its length num_str = str(number) num_len = len(num_str) # initialize sum variable to 0 sum = 0 # loop through each digit in the number for digit in num_str: # raise the digit to the power of the number of digits sum += int(digit) ** num_len # check if the sum is equal to the original number return sum == number # get input from user number = int(input("Enter a number: ")) # check if the number is an Armstrong number if is_armstrong(number): print(number, "is an Armstrong number") else: print(number, "is not an Armstrong number")
In this program, the is_armstrong()
function checks if a given number is an Armstrong number, using the same algorithm as in the previous program.
In the main part of the program, we get input from the user using the input()
function, and convert it to an integer using int()
. We then call the is_armstrong()
function to check if the number is an Armstrong number, and print out the result using print()
.