# Calculation of Armstrong Number in Python

03 Jul 2024
Beginner
444 Views

## What is an Armstrong Number?

Have you learned about Armstrong numbers in mathematics? Do you have any idea regarding the numbers whose manipulation of digits gives that number itself? A positive number where the sum of its digits each raised to the power of the number of digits equals the number itself is known as an Armstrong number.

In this Python tutorial, we'll learn how to check an Armstrong number in Python, methods to calculate Armstrong number, applications of it, etc.

## Mathematical Representation of Armstrong Number

If we have a positive number of n digits, the Armstrong number of order n is calculated as

``````
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) +...
``````

Here order denotes the number of digits.

For example, 370 is an Armstrong number because

370 = 3*3*3 + 7*7*7 + 0*0*0

Here, the number is of 3 digits.

## Approaches to Check Armstrong Number in Python

### 1. Digit by Digit Sum Approach to Check for Armstrong Number

``````
import math

def isArmstrong(num):
n = num
numDigits = 0
sum = 0

# Find number of digits in num
while n > 0:
n //= 10
numDigits += 1

n = num

# Calculate sum of digits raised to the power of numDigits
while n > 0:
digit = n % 10
sum += math.pow(digit, numDigits)
n //= 10

# Check if num is Armstrong number or not
if sum == num:
return True
return False

num1 = 407
if isArmstrong(num1):
print(num1, "is an Armstrong number.")
else:
print(num1, "is not an Armstrong number.")

num2 = 372
if isArmstrong(num2):
print(num2, "is an Armstrong number.")
else:
print(num2, "is not an Armstrong number.")
``````

In the above code, first of all, in the isArmstrong() function, we have found the number of digits in the given number and stored it in the numDigits variable. Then we extracted each digit from the given numbers num1 and num2, raised it to the power of the number of digits, and added it to the sum variable. If the sum evaluates to the given number, then it is an Armstrong number, else it is not.

#### Output

``````407 is an Armstrong number.
372 is not an Armstrong number.
``````

### 2. Using the return statement

``````
def is_armstrong_number(number):
return sum(int(digit)**len(str(number)) for digit in str(number)) == number

number = 1634
if is_armstrong_number(number):
print(f"{number} is an Armstrong number")
else:
print(f"{number} is not an Armstrong number")
``````

In the above code, the function is_armstrong_number calculates the sum of each digit raised to the power of the number of digits in the number using a generator expression and the sum() function. It then compares this sum to the original number to determine if it's an Armstrong number.

#### Output

``````1634 is an Armstrong number
``````

### 3. Check Armstrong number of n digits using a while loop

``````
number = 54748

# Changing num variable to string
order = len(str(number))

sum = 0

temp = number
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10

if number == sum:
print(number,"is an Armstrong number")
else:
print(number,"is not an Armstrong number")
``````

#### Output

``````54748 is an Armstrong number
``````
 Read More: while loop in Python

### 4. Check Armstrong number of n digits using functions

``````
# Function to calculate x raised to the power y
def power(a, b):

if b == 0:
return 1
if b % 2 == 0:
return power(a, b // 2) * power(a, b // 2)

return a * power(a, b // 2) * power(a, b // 2)

# Function to calculate the order of the number
def order(a):

# Variable to store the number
n = 0
while (a != 0):
n = n + 1
a = a // 10

return n

# Function to check whether the given number is an Armstrong number or not
def isArmstrong(a):

n = order(a)
temp = a
sum1 = 0

while (temp != 0):
r = temp % 10
sum1 = sum1 + power(r, n)
temp = temp // 10

# If condition satisfies
return (sum1 == a)

# Driver code
a = 370
print(isArmstrong(a))

a = 1253
print(isArmstrong(a))    ``````

In the above code, power(a, b) is a recursive function that calculates the power of a number a raised to the exponent b. The order(a) function calculates the order of a number, i.e., the number of digits in it whereas isArmstrong(a) function checks if the number a is an Armstrong number by summing the powers of its digits and comparing it to the original number.

#### Output

``````True
False   ``````

### 5. Check Armstrong number of n digits using Recursion

``````
def getSum(num):
if num == 0:
return num
else:
return pow((num % 10), order) + getSum(num // 10)

num = 371

# Order of the input
order = len(str(num))

# Call the function
sum_val = getSum(num)

if sum_val == num:
print('It is an Armstrong number')
else:
print('It is not an Armstrong number')
``````

The getSum() function in the above program is defined to calculate the sum of each digit raised to the power of the number of digits in the number recursively.

#### Output

``````It is an Armstrong number
``````

## Finding Armstrong Numbers within a given range in Python

``````
lower = 100
upper = 1000

for num in range(lower, upper + 1):

# order of number
order = len(str(num))

# initialize sum
sum = 0

temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10

if num == sum:
print(num)``````

The above program iterates over each number in the specified range (100 to 1000).

#### Output

``````153
370
371
407
``````

### Python Program to Find Armstrong Number in an Interval by Importing math Module

``````
import math

first, second = 1500, 10000

def is_Armstrong(val):
armstrong_sum_value  = 0
temp_list = [int(d) for d in str(val)]

for i in range(0, len(temp_list)):
armstrong_sum_value = armstrong_sum_value + math.pow(temp_list[i], len(temp_list))

if   armstrong_sum_value  == val:
print(str(val) + ", ", end="")

for i in range(first, second + 1):
is_Armstrong(i)
``````

In the above module, the math.pow() function from the math module is used to compute the power of a given number.

#### Output

``````1634, 8208, 9474,
``````

## Practical Examples and Use Cases of Armstrong Number

• Error Detection: Armstrong numbers can be used in error detection techniques, especially in data transmission and storage systems, where ensuring data integrity is crucial.
• Security: Armstrong numbers can serve as part of encryption keys or hash functions, enhancing security measures by introducing complexity and randomness into cryptographic systems.
• Data Analysis: Armstrong numbers can be used as keys in databases or as markers in datasets to categorize or filter data based on specific criteria.
• Testing Algorithms: Armstrong numbers provide a useful benchmark for testing and evaluating algorithms, especially those related to number theory and mathematical operations.
• Performance Testing: Armstrong numbers can be utilized in performance testing scenarios to evaluate the computational efficiency of algorithms or systems.
• Puzzle and Game Design: Armstrong numbers can be incorporated into puzzle and game design to create challenges or quests that require players to identify or generate Armstrong numbers.
##### Summary

Armstrong number is an important concept as it has wide applications. The above article covers various approaches to checking an Armstrong number and finding it in a specific range. You need to understand it and practice it.

## FAQs

### Q1. What are Armstrong numbers in Python?

A positive number where the sum of its digits each raised to the power of the number of digits equals the number itself is known as an Armstrong number.

### Q2. Is 371 an Armstrong number?

Yes, 371 is an Armstrong number.

### Q3. How is 153 an Armstrong number?

153 = 1*1*1 + 5*5*5 + 3*3*3

### Q4. How can I get my Armstrong number between 1 to 500?

To find Armstrong numbers between 1-500 in Python, you can use a loop to iterate through each number in the range and check if it satisfies the Armstrong number condition.
Share Article

### Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
 Microsoft Azure Cloud Architect Aug 11 SAT, SUN Filling Fast03:00PM to 05:00PM (IST) ASP.NET Core Certification Training Aug 11 SAT, SUN Filling Fast09:30AM to 11:30AM (IST) Full-Stack .NET Developer Certification Training Aug 18 SAT, SUN Filling Fast08:30PM to 10:30PM (IST) ASP.NET Core Certification Training Aug 23 MON, WED, FRI Filling Fast07:00AM to 08:30AM (IST) ASP.NET Core Project Aug 24 SAT, SUN Filling Fast07:00AM to 09:00AM (IST) React Certification Training Aug 25 SAT, SUN Filling Fast06:00PM to 08:00PM (IST) Advanced Full-Stack .NET Developer Certification Training Sep 01 SAT, SUN Filling Fast08:30PM to 10:30PM (IST) ASP.NET Core Certification Training Sep 01 SAT, SUN Filling Fast08:30PM to 10:30PM (IST) .NET Microservices Certification Training Sep 08 SAT, SUN Filling Fast05:30PM to 07:30PM (IST) .NET Solution Architect Certification Training Sep 08 SAT, SUN Filling Fast05:30PM to 07:30PM (IST)

Can't find convenient schedule? Let us know