# Understanding Armstrong Numbers in Python: Definition, Explanation, and Examples armstrong number in python

When learning programming, it is important to understand the basic concepts and logic behind various types of mathematical calculations. One of these concepts is the Armstrong number. An Armstrong number is a number whose sum of cubes of its digits is equal to armstrong number in python the number itself. This article will explain the definition, characteristics, and properties of Armstrong numbers and demonstrate how to identify them using Python.

## What are Armstrong Numbers?

An Armstrong number is a number whose sum of cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number because:

1^3 + 5^3 + 3^3 = 153

Armstrong numbers are named after Michael F. Armstrong, who introduced this concept in the year 1969.

## Characteristics of Armstrong Numbers

There are a few characteristics that are common among Armstrong numbers. These characteristics include:

1. The number of digits in an Armstrong number is always greater than one.
2. The sum of cubes of individual digits of the number is equal to the number itself.
3. An Armstrong number can be calculated for any base.

## How to identify Armstrong numbers using Python

Python is a high-level programming language that can be used to identify Armstrong numbers. There are several ways to write Python code to identify armstrong number in python. Here are a few methods:

Using a loop

We can use a loop to iterate through all the numbers in the range and check if they are Armstrong numbers. Here is the Python code:

```pythonCopy code```# Python program to check if a number is an Armstrong number

# take input from the user
num = int(input("Enter a number: "))

# initialize sum
sum = 0

# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")``````
``````

In this code, we first take input from the user and initialize the sum variable to zero. We then use a while loop to calculate the sum of the cube of each digit in the number. Finally, we check if the sum is equal to the number itself and display the result.

Using recursion

We can also use recursion to identify Armstrong numbers. Here is the Python code:

``````# Python program to check if a number is an Armstrong number using recursion

# define the recursive function
def is_armstrong(num):
if num < 10:
return num
else:
return (num % 10) ** 3 + is_armstrong(num // 10)

# take input from the user
num = int(input("Enter a number: "))

# check if the number is an Armstrong number
if num == is_armstrong(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
``````

In this code, we define a recursive function is_armstrong() that takes a number as input and returns the sum of the cube of its digits. We then take input from the user and check if the number is an Armstrong number using the is_armstrong() function.

Using a list comprehension

We can also use a list comprehension to identify Armstrong numbers. Here is the Python code:

``````Python program to check if a number is an Armstrong number using list comprehension

# take input from the user
num = int(input("Enter a number: "))

# calculate the sum``````

In this code, we use a list comprehension to calculate the sum of the cube of each digit in the number. We then check if the sum is equal to the number itself and display the result.

## Examples of Armstrong numbers

Let’s look at a few examples of Armstrong numbers in Python:

Example 1: 153

``````# Python program to check if a number is an Armstrong number

# take input from the user
num = 153

# initialize sum
sum = 0

# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
``````

Output: 153 is an Armstrong number

Example 2: 371

``````# Python program to check if a number is an Armstrong number using recursion

# define the recursive function
def is_armstrong(num):
if num < 10:
return num
else:
return (num % 10) ** 3 + is_armstrong(num // 10)

# take input from the user
num = 371

# check if the number is an Armstrong number
if num == is_armstrong(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
``````

Output: 371 is an Armstrong number

Example 3: 1634

``````# Python program to check if a number is an Armstrong number using list comprehension

# take input from the user
num = 1634

# calculate the sum
sum = sum([int(i)**3 for i in str(num)])

# display the result
if num == sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
``````

Output: 1634 is an Armstrong number

## Conclusion

In conclusion, Armstrong numbers are an important concept in mathematics and computer programming. They are numbers whose sum of cubes of individual digits is equal to the number itself. Python provides several ways to identify armstrong number in python, including loops, recursion, and list comprehension. By understanding the characteristics and properties of Armstrong numbers and using Python to identify them, programmers can better understand the basics of mathematical calculations and build more efficient and effective code.