Last Updated on July 21, 2023 by Mayank Dham

In this blog post, we will explore the concept of the Armstrong Number in Python and learn how to check if a given number is an Armstrong number using Python. We will also provide a Python program that can be used as a foundation for solving related problems. An Armstrong number is a unique kind of number that is equal to the sum of the cubes of its individual digits. For example, numbers like 0, 1, 153, and 370 are considered Armstrong numbers in Python. So, let’s delve into the details and examine the Python program for verifying an 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**

In conclusion, we have discussed the concept of Armstrong Numbers and how to check if a given number is an Armstrong Number using Python. An Armstrong Number is a number that is equal to the sum of the cubes of its digits. We provided a Python program to check for Armstrong Numbers, which can be used as a foundation for solving similar problems.

## Frequently Asked Questions (FAQs) related to Python Program to Check Armstrong Number:

Below are some frequently asked questions related to Armstrong number in Python:

**Q1. How does the Python program check for Armstrong Numbers?**

The Python program to check for Armstrong Numbers takes a user-provided number and calculates its length (the number of digits). Then, it examines each digit and computes the sum of their cubes. If the sum is equal to the original number, it confirms that the number is an Armstrong Number.

**Q2. Can I use this program to find Armstrong Numbers for any given range?**

Yes, you can modify the program to find Armstrong Numbers within a given range. By using a loop to iterate through the range of numbers, you can check each number’s Armstrong property using the same logic.

**Q3. What are some other applications of the Armstrong Number concept?**

The Armstrong Number concept is used in various fields, including cryptography and error detection. In cryptography, these numbers can be used to generate secure keys. In error detection algorithms, they help identify errors in data transmission.

**Q4. Does the program handle negative numbers or decimals as input?**

The provided Python program assumes a positive integer as input. It may require modifications to handle negative numbers or decimals, as these cases have different considerations for determining whether they are Armstrong Numbers.

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”)