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.
For example, the number 153 is an Armstrong number because:
1^3 + 5^3 + 3^3 = 153
Similarly, the number 371 is an Armstrong number because:
3^3 + 7^3 + 1^3 = 371
And the number 9474 is an Armstrong number because:
9^4 + 4^4 + 7^4 + 4^4 = 9474
So, in general, if a number n
has d
digits, it is an Armstrong number if the sum of each digit raised to the power d
is equal to n
.
Here’s a Python program that uses a function to check if a number is an Armstrong number or not:
Way 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def is_armstrong_number(n): digits = str(n) num_digits = len(digits) sum_of_digits = 0 for digit in digits: sum_of_digits = sum_of_digits + int(digit) ** num_digits return sum_of_digits == n #test print( is_armstrong_number(371)) |
Way 2:
1 2 3 4 5 6 7 8 9 10 11 12 | def is_armstrong_number(n): # Convert the number to a string to count the digits digits = str(n) num_digits = len(digits) # Calculate the sum of the digits raised to the power of the number of digits sum_of_digits = sum(int(digit)**num_digits for digit in digits) # Check whether the sum of the digits is equal to the original number return sum_of_digits == n |
Way 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def is_armstrong(num): # find the sum of the cube of each digit sum = 0 temp = num while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 # return the result return num == sum # take input from the user num = int(input("Enter a number: ")) # call the function and print the result if is_armstrong(num): print(num, "is an Armstrong number") else: print(num, "is not an Armstrong number") |
Output:
1 2 3 4 | Enter a number: 153 153 is an Armstrong number |
Explanation line by line:
def is_armstrong(num):
: This line defines a function namedis_armstrong
that takes a single argumentnum
.sum = 0
: This line initializes thesum
variable with 0, which will be used to store the sum of the cubes of each digit.temp = num
: This line creates a copy of thenum
variable and stores it intemp
.while temp > 0:
: This line starts a while loop that continues untiltemp
is greater than 0.digit = temp % 10
: This line finds the last digit oftemp
by using the modulo operator (%
), and stores it in thedigit
variable.sum += digit ** 3
: This line adds the cube ofdigit
to thesum
variable.temp //= 10
: This line updates thetemp
variable by removing the last digit, by using integer division (//
).return num == sum
: This line returns the result of the comparison betweennum
andsum
.num = int(input("Enter a number: "))
: This line takes input from the user as an integer and stores it in the variablenum
.if is_armstrong(num):
: This line calls theis_armstrong
function and checks its return value.print(num, "is an Armstrong number")
: If the condition in line 10 is true, this line prints the message indicating thatnum
is an Armstrong number.print(num, "is not an Armstrong number")
: If the condition in line 10 is false, this line prints the message indicating thatnum
is not an Armstrong number.