Check Armstrong Number in Python

Check Armstrong Number in Python: An Armstrong number (also known as a narcissistic number or a pluperfect digital invariant) is a number that is equal to the sum of its own digits raised to the power of the number of digits.

In other words, an n-digit number is an Armstrong number if the sum of its digits, each raised to the power of n, equals the number itself

153:

  • Number of digits (n) = 3.
  • Sum of its digits raised to the power of 3: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
  • 153 is equal to the sum of its digits raised to the power of 3, so it’s an Armstrong number.

Python Program to check Armstrong number

Algorithm

  1. Start
  2. Define a custom function isArmstrongNumber()
  3. Accept an integer input from the user and store it in the variable num.
  4. Define a function called isArmstrongNumber that takes an integer num as an argument.
  5. Convert the integer num to a string and store it in the variable numStr.
  6. Determine the total number of digits in the input number by calculating the length of numStr and store it in the variable totalDigits.
  7. Initialize a variable called armstrongSum to zero, which will be used to accumulate the sum of each digit raised to the power of totalDigits.
  8. Iterate through each digit character in the numStr using a for loop, where digitStr represents the current digit as a string.
  9. Convert the digitStr back to an integer and store it in the variable digit.
  10. Calculate the result of digit raised to the power of totalDigits and add it to the armstrongSum.
  11. After the loop, check if armstrongSum is equal to the original input number num.
  12. If armstrongSum is equal to num, print that num is an Armstrong number; otherwise, print that num is not an Armstrong number.
  13. Finally, outside the function, call the isArmstrongNumber function with the user’s input number num and display the result.
  14. End

Source code

The given Python code defines a function isArmstrongNumber(num) that checks whether a given number is an Armstrong number or not.

def isArmstrongNumber(num):
    numStr = str(num)
    totalDigits = len(numStr)
    
    armstrongSum = 0
    
    for digitStr in numStr:
        digit = int(digitStr)
        armstrongSum += digit ** totalDigits
    
    return armstrongSum == num

num = int(input("Enter a number: "))

if isArmstrongNumber(num):
    print(f"{num} is an Armstrong number.")
else:
    print(f"{num} is not an Armstrong number.")

Steps to Write Program

Step-1: Define a custom function with a single argument

isArmstrongNumber(num):

Step-2: Convert the input number to a string:

numStr = str(num)

Step-3: Calculate the number of digits in the number:

totalDigits = len(numStr)

Step-4: Initialize a variable armstrongSum to store the sum of the digits raised to the power of num_digits:

armstrongSum = 0

Step-5: Iterate through each digit in the number using a for loop:

for digitStr in numStr:

Step-6: Convert each digit from a string to an integer:

digit = int(digitStr)

Step-7: Add the digit raised to the power of totalDigits to armstrongSum:

armstrongSum += digit ** totalDigits

Step-8: After the loop finishes, check if armstrongSum is equal to the original input number:

return armstrongSum == num

Step-9: Declare the num variable and assign an input field with int() to accept only the integer value

num = int(input("Enter a number: "))

Step-10: call IsArmstrongNumber(num) with if condition to check the Armstrong number

if isArmstrongNumber(num):

Step-11: If step-10 returns true then print the given number is an Armstrong number within the if block

print(f"{num} is an Armstrong number.")

Step-12: If step-10 returns false then print the given number is not an Armstrong number within the else: block

else:
    print(f"{num} is not an Armstrong number.")

Output

If you input the number 153, which is an Armstrong number

Enter a number: 153
153 is an Armstrong number.

If you input a number like 123, which is not an Armstrong number:

Enter a number: 123
123 is not an Armstrong number.