In this blog, we will discuss what is an Armstrong Number in python, How to check if the given number is Armstrong in Python. A Python program to check Armstrong number will help in building the logicals for solving the problems. The Armstrong number is a number that is equal to the sum of the cubes of its digits. For example 0, 1, 153 and 370 are the Armstrong numbers. Let’s deep dive and see the python program to check armstrong number.

## What is Armstrong Number in python?

A positive integer will be known as an Armstrong number in python of order n if

abcd… = a**n + b**n + c**n + d**n + …

In case of an Armstrong number of 3 digits, the sum of cubes of each digit must be equal to the number itself. For example:

371 = 3*3*3 + 7*7*7 + 1*1*1 = 27 + 343 + 1 // 153 is an Armstrong number.

## 1. Check Armstrong Number in Python Using While Loop

- Given a number num, count the length of the number and store it in a variable n1 and initialize variables sum with a value 0 and temp with a value num.
- Run a while loop with a condition if temp>0 and perform the following operations in it.
- Initialize cur_dig with value temp%10 i.e. it will be the last digit of the temp number.
- Add cur_dig**n1 in sum.
- Update the value of temp to temp//10.

- At last check if num == sum, if it is True then print “Number is an Armstrong” else “Number is not Armstrong”.

### Python Program to check Armstrong Number using while loop

num = int(input("Enter a number: ")) sum = 0 n1 = len(str(num)) temp = num while temp > 0: digit = temp % 10 sum += digit ** n1 temp //= 10 if num == sum: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number")

```
Output
371 is an Armstrong number
```

**Time Complexity of armstrong number in python**

We are looking at each digit of the number, therefore time complexity will be O(X), where X = log10(N) and N is the number which we are checking for being an Armstrong number.

**Space Complexity of armstrong number in python**

As We are not using any extra space, therefore the time complexity will be O(1).

## 2. Checking Armstrong Number in Python Using Recursion

- Take input of integer in num, initialize sum =0, n1 = len(num), and temp equal to num.
- Make one function check_armstrong to check if the number is armstrong or not and pass 4 parameters num, n1, sum and temp.
- In function perform the following steps:
- Check the base condition if temp==0. Then,
- If sum == num return True
- Else return False

- Initialize digit with value temp%10
- Add digit**n1 in sum.
- Update the value of temp to temp//10.
- Atlast return the function call with num, n1, sum, temp as parameters.

- Check the base condition if temp==0. Then,

### Code Implementation to check Armstrong Number in python Using Recursion

def check_armstrong(num,n1,sum,temp): if temp==0: if sum==num: return True else: return False digit = temp % 10 sum = sum + digit**n1 temp = temp//10 return check_armstrong(num, n1, sum, temp) num = int(input("Enter a number: ")) sum = 0 n1 = len(str(num)) temp = num res = check_armstrong(num,n1,sum,temp) if res: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number")

```
Output
370 is an Armstrong number
```

**Time Complexity of armstrong number in python**

We are looking at each digit of the number, therefore time complexity will be O(X), where X = log10(N) and N is the number which we are checking for being an Armstrong number.

**Conclusion**

This blog has deeply discussed the python program to check Armstrong number. Coding questions like finding if the given number is an armstrong number are generally asked in the service based companies like TCS, Wipro, Accenture, Capegimini, Infosys and many more. Practice more on logical questions like Finding Armstrong number will increase your logic building skills.

**Other Python Programs**

Python program to reverse a number

Python program for heap sort

Python program to add two numbers

Python program to check leap year

Python program to convert celsius to fahrenheit

Python program to find factorial of a number

Python program to find the middle of a linked list using only one traversal

Python program to reverse a linked list

a=370

sum=0

n=len(str(a))

for digit in str(a):

# digit**n

print(int(digit)**n)

sum+=int(digit)**n

if sum==a:

print(“Armstrong”)

else:

print(“its not”)