# Python Program to Check for Armstrong Number

## What is an Armstrong number?

An Armstrong number is an n-digit number such that the sum of it digits raised to the power n is the number itself. For example, 153 is a 3 digit Armstrong number since,

153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Another example is the 4 digit Armstrong number - 1634,

1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634

Armstrong numbers are also known as narcissistic numbers in recreational mathematics.

## Python Program to Check for Armstrong Numbers

The following python program checks whether a given number is an Armstrong number. It also prints the order of the Armstrong number.

number = int(input("Please enter a number: ")) num_str = str(number) num_len = len(num_str) sum_of_digits = 0 for d in num_str: sum_of_digits += int(d) ** num_len # power operation if number == sum_of_digits: print("{} is an Armstrong number of order {}".format(number,num_len)) else: print("{} is NOT an Armstrong number".format(number))

Following are the steps in the above program,

- We first accept an input from user and convert it into a number
- We convert the number to a string and find its length
- We then iterate through each digit character and then find the sum of the digits raised to the power of length of the string(which is the number of digits in the number)
- Finally we check whether the sum is same as the original number. If they are same, this number is an Armstrong number!

## Python Program to Print All 3 Digit Armstrong Numbers

The following python program prints all the 3 digit Armstrong numbers. We have extracted the logic used in the above program to a separate function named is_arm(),

def is_arm(number): num_str = str(number) num_len = len(num_str) sum_of_digits = 0 for d in num_str: sum_of_digits += int(d) ** num_len # power operation if number == sum_of_digits: return True else: return False for n in range(100,1000): # 100 to 999 if is_arm(n): print("{} is an Armstrong number of order {}".format(n,len(str(n))))

Following is the output from the above python program,

153 is an Armstrong number of order 3

370 is an Armstrong number of order 3

371 is an Armstrong number of order 3

407 is an Armstrong number of order 3

## Python Program to Print All Armstrong Numbers Below a Specified Number

The following python program prints all Armstrong numbers below a specified number. It also prints the order of the Armstrong number.

def is_arm(number): num_str = str(number) num_len = len(num_str) sum_of_digits = 0 for d in num_str: sum_of_digits += int(d) ** num_len # power operation if number == sum_of_digits: return True else: return False limit = int(input("Please enter the limit for Armstrong number check: ")) for n in range(1,limit+1): # 1 to limit (inclusive) if is_arm(n): print("{} is an Armstrong number of order {}".format(n,len(str(n))))

Following is the sample output from the above python program,

Please enter the limit for Armstrong number check: 9999

1 is an Armstrong number of order 1

2 is an Armstrong number of order 1

3 is an Armstrong number of order 1

4 is an Armstrong number of order 1

5 is an Armstrong number of order 1

6 is an Armstrong number of order 1

7 is an Armstrong number of order 1

8 is an Armstrong number of order 1

9 is an Armstrong number of order 1

153 is an Armstrong number of order 3

370 is an Armstrong number of order 3

371 is an Armstrong number of order 3

407 is an Armstrong number of order 3

1634 is an Armstrong number of order 4

8208 is an Armstrong number of order 4

9474 is an Armstrong number of order 4