# Python Program for Armstrong Numbers

## Python Program to Check Armstrong Number

We have to write a program that will determine if a given number x is Armstrong or not in python. A positive integer x of n digits is called an Armstrong number of order n. Order is the number of digits in x.Formula to check if a number is Armstrong or not

where p, q, r, s,... are single digits in a numberpqrs... = pow(p,n) + pow(q,n) + pow(r,n) + pow(s,n) + ....

pow() is power function in python

and n is the order i.e count of digits in x.

This formula simply states that sum of powers of each digit raise to order n is equal to the number itself.

### Example:

Take X = LHS = 153 There are three digits in number so order n = 3 Calculate RHS of formula pow(1,3) + pow(5,3) + pow(3,3) = 1 + 125 + 27 = 153 = LHS Hence 153 is Armstrong number.

### Python Code to check Armstrong Number

```
# Function to calculate order (count of digits) of the number
def order(x):
# variable to store count of digits
# in a number
n = 0
while (x!=0):
n = n+1
x = x/10
return n
# Function to check whether the given number
# is Armstrong number or not
def isArmstrong (x):
n = order(x)
temp = x
sum1 = 0
while (temp!=0):
r = temp%10
sum1 = sum1 + pow(r, n)
temp = temp/10
# If condition satisfies
return (sum1 == x)
# Driver Program
# set number to check as armstrong or accept
# x = input("Enter a number:- ")
x = 153
print(isArmstrong(x))
x = 2826
print(isArmstrong(x))
x = 54748
print(isArmstrong(x))
```

**Output**

True False True

## Python Program to Find Armstrong Numbers in an Interval

Que. Write a python program to find armstrong numbers between the given range.```
# Function to calculate order (count of digits) of the number
def order(x):
# variable to store count of digits
# in a number
n = 0
while (x!=0):
n = n+1
x = x/10
return n
# Function to check whether the given number
# is Armstrong number or not
def isArmstrong (x):
n = order(x)
temp = x
sum1 = 0
while (temp!=0):
r = temp%10
sum1 = sum1 + pow(r, n)
temp = temp/10
# If condition satisfies
return (sum1 == x)
# Driver Program
# python code to find armstrong numbers
# within a range i.e between two numbers
x = 1
y = 1000
print("Armstrong numbers between 1 to 1000 are:")
for i in range(x,y+1):
if(isArmstrong(i)):
print(i)
```

**Output:**

Armstrong numbers between 1 to 1000 are: 1 2 3 4 5 6 7 8 9 153 370 371 407

**Related Links**

C program for Armstrong number

Narcissistic numbers are also known as Armstrong numbers. You can find more about them at Wikipedia. Narcissistic number