Check Armstrong Number in Python Using Function

(Last Updated On: 12/12/2022)

Python Program to Check Armstrong Number Using Function

# Check Armstrong Number in Python Using Function

# import python math library
from math import *

# get input from user
num = int(input("Enter the number : "))

# get input number length
n = len(str(num))
temp = num

result = 0
while (temp != 0):
    # logic to calculate armstrong number
    remainder = temp % 10
    result = result + pow(remainder, n)
    temp = int(temp/10)

# display output
if (result == num):
    print("The number is an Armstrong number")
else:
    print("The number is not an Armstrong number")

Output:

Enter the number : 153
The number is an Armstrong number
Enter the number : 155
The number is not an Armstrong number

Leave a Reply

Your email address will not be published. Required fields are marked *