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
- Start
- Define a custom function isArmstrongNumber()
- Accept an integer input from the user and store it in the variable
num. - Define a function called
isArmstrongNumberthat takes an integernumas an argument. - Convert the integer
numto a string and store it in the variablenumStr. - Determine the total number of digits in the input number by calculating the length of
numStrand store it in the variabletotalDigits. - Initialize a variable called
armstrongSumto zero, which will be used to accumulate the sum of each digit raised to the power oftotalDigits. - Iterate through each digit character in the
numStrusing a for loop, wheredigitStrrepresents the current digit as a string. - Convert the
digitStrback to an integer and store it in the variabledigit. - Calculate the result of
digitraised to the power oftotalDigitsand add it to thearmstrongSum. - After the loop, check if
armstrongSumis equal to the original input numbernum. - If
armstrongSumis equal tonum, print thatnumis an Armstrong number; otherwise, print thatnumis not an Armstrong number. - Finally, outside the function, call the
isArmstrongNumberfunction with the user’s input numbernumand display the result. - 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.