Python Program to Check Armstrong Number

Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits

Formulae of Amstrong number is

ABC = A^ 3 + B^ 3 + C^ 3

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

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

= 153

In this article, we will see a Python program to check whether the input number Armstrong or not

Example 1: Checking Amstrong number using Python While loop

# get the input number
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 example, we ask the user to enter a number and then check whether it is an Armstrong number by finding the sum of the cube of each digit.
If the sum of each digit is equal to the number itself then we output “The entered number is an Armstrong number” otherwise we print “The entered number is not an Armstrong number”.
For instance, if the user enters 153, the output would be “The entered number is an Armstrong number”.

Output

Enter a number: 153
The entered number is an Armstrong number

Example 2: Using Recursion to check Armstrong number in Python

# define a recursive function to find the sum of the cube of each digit
def armstrong(num):
    if num == 0:
        return 0
    else:
        return (num%10)**3 + armstrong(num//10)

# get the input number
num = int(input("Enter a number: "))

# find the sum of the cube of each digit using the recursive function
sum = armstrong(num)

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

Output

Enter a number: 9474
The input number is an Armstrong number

Example 3: Using list comprehension to check an Amstrong number

# get the input number
num = int(input("Enter a number: "))

# get the digits of the number as a list
digits = [int(d) for d in str(num)]

# find the sum of the cube of each digit
sum = sum([d**3 for d in digits])

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

In this example we take an input number from the user and splits it into individual digits using Python list comprehensions.

After splitting we, computes the cube of each digit and sum the result of each cube.

If the final sum of each cube digit is equal to the input number, we print “The given number is an Armstrong number” otherwise we print “The given number is not an Armstrong number”.

Output

Enter a number: 9474
The input number is an Armstrong number

Armstrong number use cases

Cryptography:

Armstrong numbers can be utilized in cryptography to generate secure keys. A key can be produced by randomly selecting an Armstrong number of a specific order and then using this key for encryption and decryption.

Error detection:

Armstrong numbers can be used to identify errors in data transmission, particularly when the transmitted data is a large number. By computing the sum of the nth power of each digit of the number and comparing it with the original number, we can determine whether the transmitted data is corrupted or not.

Recreational mathematics

Armstrong numbers can be employed in recreational mathematics as they present an interesting puzzle to solve. For instance, students can be tasked with finding all Armstrong numbers of a certain order and then analyzing the patterns in these numbers.

Testing programming skills

Armstrong numbers can be used as a test case to assess the programming skills of developers. By asking developers to write a program that checks whether a given number is an Armstrong number or not, we can evaluate their understanding of loops, conditions, and other programming concepts.

Optimization algorithms:

Armstrong numbers can be utilized in optimization algorithms, particularly in cases where we need to determine the optimal value of a particular function. By searching for Armstrong numbers of a specific order, we can efficiently discover the optimal value of the function and thereby solve complex optimization problems.

Leave a Comment