Check Armstrong Number in Python Using Recursion

(Last Updated On: 12/12/2022)

Python Program to Check Armstrong Number Using Recursion

# Check Armstrong Number in Python Using Recursion
import math as m

# recursive method to calculate armstrong number to power

def getSum(num, num_length):
    if num == 0:
        return num
    else:
        return m.pow((num % 10), num_length) + getSum(num//10, num_length)

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

# get input number length
num_length = len(str(num))
sum = getSum(num, num_length)

# display output
if sum == int(num):
    print(num, "is an Armstrong Number.")
else:
    print(num, "is not an Armstrong Number.")

Output:

Enter a number: 153
153 is an Armstrong Number.
Enter a number: 155
155 is an Armstrong Number.

Leave a Reply

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