Any positive integer of length n is considered an Armstrong number if the number equals the sum of each of its digits raised to the power n. The following equation captures this condition:
Where
For example,
The below Python code demonstrates how to determine if a number is an Armstrong number.
length_n = len(n)original_n = int(n)for i in n:list.append(int(i)**length_n)sum_n = sum(list)if sum_n == original_n: print(n, "is an Armstrong number")else: print(n, "is not an Armstrong number")
Enter the input below
The code assumes that a number has been inputted and stored in n
.
n
using len()
and store it in length_n
.n
as int
and store it in original_n
.for
loop to store cubes of each of the digits of n
in list
. Next, we add all the elements of list
and store the result in sum_n
.n
is an Armstrong number, that is if the sum of each of its digits’ cube equals n
itself.