# Python program to check Armstrong Number: Easy Guide

*Summary:*

Problem Statement: Write a program in Python to check if a number is Armstrong number or not.

Description: A number is Armstrong number if the sum of all its individual digits to power n (number of digits) is equal to the number.

`abcd.. = a^n+b^n+c^n+d^n+...`

Example:

```
Input: 123
Output: It is NOT Armstrong Number.
Explanation: 123 is not equal to 1^3+2^3+3^3
Input: 371
Output: It is an Armstrong Number.
Explanation: 371 is equal to 3^3+7^3+1^3
Input: 1634
Output: It is an Armstrong Number.
Explanation: 1634 is equal to 1^4+6^4+3^4+4^4
```

Now you have understood what is an Armstrong number, its time to code a program in Python to check it.

## Python Program: Check Armstrong Number

Copy

```
num = input("Enter any Number: ")
#Use sum function to find sum of (digits raised to power n) in an list
sumOfPower = sum([int(i)**(len(num)) for i in num])
if(int(num)==sumOfPower):
print("{} is armstrong number.".format(num))
else:
print("{} is Not armstrong number.".format(num))
```

Output

Enter any Number: 1634 1634 is armstrong number.

We first take the input and then calculate the sum of individual digits having power n (length of number) and print them.

Line 4 contains all the logic of the code. We have used list comprehension to create a list of digits which is the argument of `sum()`

function.

If we break the list comprehension then it gives the following code.

Copy

`[int(i)**(len(num)) for i in num] # [1*1*1*1, 6*6*6*6, 3*3*3*3, 4*4*4*4]`

It means to create a list of integers raised to the power of the total length of number. Then the `sum()`

function returns the sum of all elements in the list.

## One-Line Solution in Python (Advanced)

Copy

```
num = input("Enter any Number: ")
print(int(num)==sum([int(i)**(len(num)) for i in num]))
```

Output

Enter any Number: 1634 True

This is a short and concise solution in Python to check Armstrong number. The logic is similar as described above.

Only changed thing is that we removed the if-else statement and put the comparison inside the print statement.

If you find anything confusing let me know in the comments.

Join Our Youtube Channel

Subscribe