Armstrong number program in python with explanation

Welcome back, In this tutorial, we are going to learn how to write a program in Python to check whether a given input number is Armstrong or not.

So before moving directly on to the writing program, We will first know about what is Armstrong number.

What is Armstrong Number?

  • It is a number that is equal to the sum of its own digits raised to the power of the number of digits.
  • For eg: 153, is an Armstrong number because it has 3 digits, and 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Lets see it practically by calculating it,

Suppose I am taking 153 for an example 

First calculate the cube of its each digits

  1^3 = (1*1*1) = 1

  5^3 = (5*5*5) = 125

  3^3= (3*3*3) = 27

Now add the cube 

  1+125+27 = 153

It means 153 is an Armstrong number.

Some examples of Armstrong numbers

  1. 153 is an Armstrong number because:
    • 1^3 + 5^3 + 3^3 = 153
  2. 371 is an Armstrong number because:
    • 3^3 + 7^3 + 1^3 = 371
  3. 407 is an Armstrong number because:
    • 4^3 + 0^3 + 7^3 = 407
  4. 1634 is an Armstrong number because:
    • 1^4 + 6^4 + 3^4 + 4^4 = 1634
  5. 8208 is an Armstrong number because:
    • 8^4 + 2^4 + 0^4 + 8^4 = 8208

In this post, we will explore different logic to write a Python program that checks if a given number is an Armstrong number.

Method 1: Check Armstrong Using a While Loop

In this method, we are using a while loop to extract each digit of the input number. Also, all the power calculation of a number and sum is done in this loop. After calculating the sum, We then compare the total sum to the input number. If a match is found then the input is an Armstrong number other not an Armstrong number. Below is the code:

# Armstrong number program using while loop
num = int(input("Please give a number: "))
sum = 0 
temp = num
# count the number of digits in input
count = len(str(num)) 
# loop on each digit and calculate the sum
while temp > 0:
    digit = temp % 10
    sum += digit ** count
    temp //= 10 
# check if the number is an Armstrong or not
if num == sum:
    print("Given ",num, "is an Armstrong number")
else:
    print("Given ",num, "is not an Armstrong number")

Output:

Please give a number: 153
Given  153 is an Armstrong number

Explanation:

  • In the above program, we first take the input from the user using the input() function.
  • After taking input, converted to an integer using the int() function.
  • We created a temporary variable temp and assigned an input number to it. Also taken and initialized sum with 0. Computed the number of digits in the input number using the len() function.
  • Using a while loop to extract each digit of the input number by taking the modulo % of the number with 10. We then raise the digit to the power of the number of digits using the ** operator and add it to the running total.
  • We remove the last digit of the input number using integer division //. We repeat this process until all the digits of the input number have been processed.
  • Finally, we compare the total sum to the input number num to check if it is an Armstrong number. If they are equal, we print a “number is an Armstrong number” or else “number is not an Armstrong number”.

Method 2: Armstrong Using List Comprehension

In this method, we are using the list comprehension concept of Python to extract each digit of the input number. Also, it is used to find the power of the number of digits. To compute the sum we are using the sum() function. Here’s the code:

# Armstrong number using list comprehension
num = int(input("Please Enter a Number: "))
digits = [int(digit) for digit in str(num)]
count = len(digits)
sum = sum([digit ** count for digit in digits])
if num == sum:
    print("Given ", num, "is an Armstrong number")
else:
    print("Given ", num, "is not an Armstrong number")

Output:

Please Enter a Number: 371
Given  371 is an Armstrong number

Explanation:

  • The above code takes the input number from the user is converted it to an integer using the int() function.
  • We have used list comprehension to extract each digit of the input number by converting it to a string using the str() function. After that, we convert each character back to an integer using the int() function.
  • We computed the number of digits in the input number. Also, we stored the digits in a list called digits. After that, we computed the sum of the digits raised to the power of the number of digits using list comprehension and the sum() function.
  • After computation, we compare the final sum output to the input number. If they are equal, then print number is an Armstrong number otherwise print number is not an Armstrong number.

Conclusion:

In this post, we have seen various approach to writing Python program that checks if a given number is an Armstrong number. The first method uses a while loop, and the second method uses list comprehension. Hope you loved this tutorial.

Leave a Comment