Check Armstrong Number in Python Using Recursion

The given code checks whether a given number is an Armstrong number in Python using recursion. Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits.

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.")

For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Let’s go through the code step by step:

  1. import math as m – Import the math module and alias it as ‘m’.
  2. def getSum(num, num_length): – Define a recursive function named getSum() that takes two parameters: num and num_length. The num parameter represents the number to check for the Armstrong number, and num_length represents the number of digits in the number.
  3. if num == 0: return num – Check if the number is zero, then return the number itself.
  4. else: return m.pow((num % 10), num_length) + getSum(num//10, num_length) – If the number is not zero, return the sum of the power of the last digit of the number and the recursive call to the getSum() function with the remaining digits.
    • m.pow((num % 10), num_length) – Calculate the power of the last digit of the number with the num_length as the exponent using the pow() function of the math module.
    • num//10 – Remove the last digit of the number by integer division with 10.
  5. num = int(input(“Enter a number: “)) – Take user input for the number to check for Armstrong number and convert it into an integer.
  6. num_length = len(str(num)) – Calculate the number of digits in the input number by converting it into a string and getting its length.
  7. sum = getSum(num, num_length) – Call the getSum() function with the input number and its length as arguments and assign the returned sum to the sum variable.
  8. if sum == int(num): – Check if the calculated sum is equal to the input number after converting it into an integer.
  9. print(num, “is an Armstrong Number.”) – If the sum is equal to the input number, print that the number is an Armstrong number.
  10. else: print(num, “is not an Armstrong Number.”) – If the sum is not equal to the input number, print that the number is not an Armstrong number.

That’s the step-by-step process of the given code to check for an Armstrong number using recursion in Python.

Output:

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

In this example, the user entered the number 153. The program calculated the length of the number (which is 3 in this case) and then used a recursive function called “getSum” to calculate the sum of the cubes of the individual digits of the number raised to the power of the number’s length.

In this case, the sum would be 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153. Since the sum is equal to the original number entered by the user, the program prints out “153 is an Armstrong Number”.

Armstrong Number Usage:

An Armstrong number is a number whose sum of the cubes of its digits is equal to the number itself. This program can be used in various applications where it is required to check whether a given number is an Armstrong number or not, such as:

  1. Education: This program can be used to teach the concept of Armstrong numbers and recursion to students in computer science or mathematics classes. Teachers can demonstrate the concept of recursion by using this program and explaining how it works.
  2. Algorithm design: This program can be used as an example of how to design an algorithm to solve a specific problem. It can be used as a reference for designing similar algorithms for other problems.
  3. Testing: This program can be used to test the correctness of other programs that use the concept of Armstrong numbers. For example, if there is a program that generates a list of Armstrong numbers, this program can be used to verify if the numbers in the list are actually Armstrong numbers or not.
  4. Quality assurance: This program can be used in software testing to check if a function or module that calculates Armstrong numbers is working correctly. By comparing the output of the program with the expected output, we can identify if there are any errors in the function or module.

Overall, the “check Armstrong number in Python using recursion” program has various use cases in education, algorithm design, testing, and quality assurance.

The code checks if the input number is an Armstrong number or not by calling a recursive function ‘getSum()’ that calculates the sum of the digits raised to the power of ‘num_length’. If the calculated sum is equal to the input number, it is an Armstrong number; otherwise, it is not.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author

Leave a Reply