Write a Python Program to check if it is an Armstrong number or not and also we are going to cover the below topics.

- What is Armstrong’s number in python
- Armstrong number in Python using for loop
- How to check whether it is an Armstrong number or not in Python using a while loop
- Armstrong number in Python using recursion
- Armstrong number in Python using if else
- How we can display the Armstrong number in Python for a given range
- Armstrong number in Python without using string function
- Armstrong number in Python between 1 to 1000
- Armstrong number in Python using def function
- Armstrong number in Python using list comprehension
- 4-digit Armstrong number in Python
- Armstrong number in Python using lambda

**Table of Contents**show

## Armstrong number in python

- The sum of the cubes of an Armstrong number’s digits. The Armstrong numbers, for instance, are 0, 1, 153, 370, 371, and 407.
- In Python, the Armstrong number is a number where the product of all the digits added together equals the given number. For example, 1^3 + 5^3 + 3^3 equals 153 for a given integer.
- In order to find a 3-digit Armstrong number, we must first extract each digit from the number, multiply it three times to determine its cube, and then add all those cubes.
- We’ll now compare the sum to the supplied number. We refer to a number as being an Armstrong number if the total of its cubes equals the actual number; otherwise, it is not.

Example:

Let’s try to understand it in a mathematical way

```
370 = (3*3*3)+(7*7*7)+(0*0*0)
where:
(0*0*0)=0
(7*7*7)=343
(3*3*3)=27
So:
0+343+27=370
```

Example:

```
new_num = int(input("Enter a number: "))
i = 0
result = new_num
while result > 0:
new_digit = result % 10
i += new_digit ** 3
result //= 10
if new_num == i:
print(new_num," It is an Armstrong number")
else:
print(new_num," It is not an Armstrong number")
```

In the following given code First, we set the sum’s initial value to 0. We may find out the number’s digits individually using the **% **operator. The final digit of the original number is obtained by dividing the remainder of the number by 10 after that. After this, we use the exponent operator to take the digits’ cubes.

Finally, we may state that a number is an Armstrong number if the total is identical to the original number. While a number is not an Armstrong number if the total does not equal the original number.

Here is the Screenshot of the following given code

This is how we can find the Armstrong number in Python.

Read: Python Dictionary Methods

## Armstrong number in Python using for loop

- Let’s use a number as an example to better understand Armstrong’s number. Let n be the chosen value. Determine the number’s digit count. Each digit in the number is taken and raised to the power n.
- All of those are summed together, and if the total of all these numbers equals the number itself, then the number n is an Armstrong number. If not, then n is not an Armstrong number.
- In this example, we are going to use the concept of for loop and this program allows the user to enter the positive integer number.

**Example**:

```
new_number = int(input("Enter the Value:"))
new_val = len(str(new_number ))
m = new_number ;
total = 0
result=str(new_number )
for z in result:
new_digit =m%10
total += new_digit **new_val
m = m//10
if(total==new_number ):
print("",new_number ," It is an Armstrong number")
else:
print("",new_number ,"It is not an Armstrong number")
```

In the above code First, we set the sum’s initial value to 0. We may find out the number’s digits individually using the % operator. The final digit of the original number is obtained by dividing the remainder of the number by 10 after that. After this, we use the exponent operator to take the digits’ cubes.

Finally, we may state that a number is an Armstrong number if the total is identical to the original number. While a number is not an Armstrong number if the total does not equal the original number.

Here is the Screenshot of the following given code.

In this example, we have understood how to check whether it is Armstrong or not by using the for loop.

Read: How to create a list in Python

## Armstrong number in Python using while loop

- In this example, we will discuss the python program to check the Armstrong numbers by using a while loop.
- In Python, the Armstrong number is a number where the product of all the digits added together equals the given number.
- In this example, we will take a number and then initialize a variable to store the sum and assign the value to 0. Next, we will find the count of digits in the given number and for each digit in a number multiply it by the count of digits and add it to the sum variable.

**Example**:

```
new_num = int(input("Enter a number: "))
i = 0
result = new_num
while result > 0:
new_digit = result % 10
i += new_digit ** 3
result //= 10
if new_num == i:
print(new_num," It is an Armstrong number")
else:
print(new_num," It is not an Armstrong number")
```

You can refer to the below Screenshot

This is how we can create an Armstrong number in Python by using the while loop.

Read: Python naming conventions

## Armstrong number in Python using recursion

- Recursion is the process of defining something in terms of another item. It is a process in which a function calls itself directly or indirectly, to put it simply.
- In this example, we are going to use the concept if-else condition and this program allows the user to enter the positive integer number.

**Example**:

```
total_num=0
def check_ArmstrongNumber(m):
global total_num
if (m!=0):
total_num+=pow(m%10,3)
check_ArmstrongNumber(m//10)
return total_num
m=int(input("Enter the value:"))
if (check_ArmstrongNumber(m) == m ):
print("This is an Armstrong Number.")
else:
print("This is not an Armstrong Number.")
```

Here is the implementation of the following given code

As you can see in the Screenshot we have checked whether it is Armstrong or not by using the recursion method.

Read: Remove Unicode characters in python

## Armstrong number in Python using if else

- When a condition is true, the if-else statement’s else block and if statement is both executed.
- In this example, we will define a variable ‘new_num’ and assign the integer number also set the condition if add_sum == new_num if it is equal then it is Armstrong number otherwise it will return it as not Armstrong number.

**Example**:

```
new_num = 370
i = new_num
add_sum = 0
while i != 0:
n = i % 10
add_sum += n*n*n
i = i//10
if add_sum == new_num:
print('This is a Armstrong Number')
else:
print('This is not an Armstrong Number')
```

Here is the execution of the following given code

This is how we can create an Armstrong number in Python using the if-else condition.

Read: Comment lines in Python

## Armstrong number in Python for a given range

- Here we will discuss how to set the interval range to get the Armstrong number in Python.
- In order to find a 3-digit Armstrong number, we must first extract each digit from the number, multiply it three times to determine its cube, and then add all those cubes.

**Example**:

```
new_lower_val = int(input("Enter the lower value: "))
new_upper_val = int(input("Enter the upper value: "))
for i in range(new_lower_val,new_upper_val + 1):
total = 0
n = i
while n > 0:
digit = n % 10
total += digit ** 3
n //= 10
if i == total:
print(i)
```

In the following given code, we have used the input function and used for loop method to iterate from variable lower to upper. In iteration, the number of lower is increased by 1 and checked whether it is an Armstrong number or not.

Here is the implementation of the following given code

In this example, we have understood how we can check the Armstrong number between the given interval or range.

Read: Check if a list is empty in Python

## Armstrong number in Python without using string function

- In this section, we will discuss how to create an Armstrong number in Python without using a string function.
- if I have a 3-digit number, I increase each digit to the power of three and add them to create the number. We refer to the number obtained as an Armstrong number if it is equal to the original number.
- The special characteristic of Armstrong numbers is that they can be a part of any number system base. For instance, the number 153 in the decimal system is an Armstrong number.

**Example**:

```
new_number=int(input("Enter any value: "))
val1=list(map(int,str(new_number)))
val2=list(map(lambda y:y**3,val1))
if(sum(val2)==new_number):
print("It is an armstrong number ")
else:
print("It is not an arsmtrong number ")
```

In the following code, we first used the input function and contained it into a variable ‘new_number’. Each digit of the number is extracted using the map function, which then transforms it into a string and stores them in the list.

It will check the condition if the sum of the cube of numbers is equal to the input number, the number is an Armstrong number otherwise it will return it is not an Armstrong number.

Here is the Screenshot of the following given code.

This is how we can check whether the number is Armstrong or not without using the string function.

Read: Python square a number

## Armstrong number in Python between 1 to 1000

- Here we will discuss finding the Armstrong number between 1 to 1000 in Python.
- In order to find a 3-digit Armstrong number, we must first extract each digit from the number, multiply it three times to determine its cube, and then add all those cubes.
- In this example, we have used the input function and used for loop method to iterate from variable lower to upper. In iteration, the number of lower is increased by 1 and checked whether it is an Armstrong number or not.
- We have set the lower limit value of 1 and the upper limit as 1000.

```
new_lower_val = int(input("Enter the lower value: "))
new_upper_val = int(input("Enter the upper value: "))
for i in range(new_lower_val,new_upper_val + 1):
total = 0
n = i
while n > 0:
digit = n % 10
total += digit ** 3
n //= 10
if i == total:
print("These numbers are armstrong:",i)
```

Here is the implementation of the following given code

As you can see in the Screenshot we have displayed the Armstrong number between 1 to 1000.

Read: Python convert tuple to list

## Armstrong number in Python using def function

The def keyword in Python is used to define a function; it is prefixed with a user-supplied function name to construct a user-defined function.

**Example**:

```
def total(new_number):
if new_number == 0:
return new_number
else:
return pow((new_number%10),result) + total(new_number//10)
new_number = int(input("Enter the value:"))
result = len(str(new_number))
new_val = total(new_number)
if new_val==new_number:
print('This is an Armstrong number')
else:
print('This is not an Armstrong number')
```

In the following given code first, we declare a** total()** function that accepts an integer argument –**new_number** which multiplies each digit’s value by the order value to calculate the sum of the digits.

The number of digits in the input number is the order value. The variable order will be used to hold this value. The function is then invoked again, and if the sum matches the input value, an Armstrong number is returned.

You can refer to the below Screenshot.

This is how we can declare an Armstrong number by using the def function.

Read: Get current directory Python

## Armstrong number in Python using list comprehension

- To iterate over each element in the Python list, a comprehension is made out of brackets carrying the expression, which is then run for each element.
- In this example, we are going to use the list comprehension method to check whether it is an Armstrong number or not.

**Example**:

```
new_number=int(input("Enter any value: "))
val1=list(map(int,str(new_number)))
val2=list(map(lambda y:y**3,val1))
if(sum(val2)==new_number):
print("It is an armstrong number ")
else:
print("It is not an arsmtrong number ")
```

In the following given code first, we have used the input function and stored it in a variable. Each digit of the number is extracted using the map function, which then transforms it into a string and puts it in a list, and the second map function cubes each number and stores it in another list.

It will check the condition if the sum of the cube of digits is equal to the original number, the number is an Armstrong number otherwise it will return it is not an Armstrong number.

Here is the Screenshot of the following given code

In this example, we have displayed the Armstrong number by using the list comprehension method.

Read: Create an empty array in Python

## 4-digit Armstrong number in Python

- Each digit must be raised to the power of four individually before the total of all the digits is added. It is said to be an Armstrong number if the total equals the original value; otherwise, it is not. 1634, 8208, and 9474 are a few examples of 4-digit Armstrong numbers.
- Two parameters are required to determine whether or not two numbers are Armstrong numbers in Python. The number of digits will be the first parameter, and the sum of the individual digits raised to the power of n will be the second.

**Example**:

```
num = 8208
new_word = len(str(num))
new_val = num
total = 0
while new_val!= 0:
i = new_val % 10
total += i**new_word
new_val = new_val//10
if total == num:
print('This is an Armstrong Number')
else:
print('This is not a Armstrong Number')
```

You can refer to the below Screenshot

This is how we can check whether it is a 4-digit Armstrong number or not in Python.

Read: Invalid syntax in python

## Armstrong number in Python using lambda

- Python functions without names are called lambda functions, also referred to as anonymous functions. The def keyword is typically used with the function’s name when declaring or defining a function.
- However, while defining lambda functions, we first employ the lambda keyword (thus the term lambda function).

**Example**:

```
new_number=int(input("Enter any value: "))
val1=list(map(int,str(new_number)))
val2=list(map(lambda y:y**3,val1))
if(sum(val2)==new_number):
print("It is an armstrong number ")
else:
print("It is not an arsmtrong number ")
```

Here is the implementation of the following given code

Also, take a look at some more Python tutorials.

In this article, we have discussed whether we can check whether it is an Armstrong number or not, and we have covered the topics below.

- Armstrong number in Python using for loop
- Armstrong number in Python using while loop
- Armstrong number in Python using recursion
- Armstrong number in Python using if else
- Armstrong number in Python for a given range
- Armstrong number in Python without using string function
- Armstrong number in Python between 1 to 1000
- Armstrong number in Python using def function
- Armstrong number in Python using list comprehension
- 4-digit Armstrong number in Python
- Armstrong number in Python using lambda

Arvind is currently working with TSInfo Technologies as a Senior Python Developer. He is proficient in Python libraries like NumPy, and Tensorflow.