Armstrong Number In Python

Armstrong Number In Python

Published on July 21 2021

In this tutorial, you will learn how to check if the given number is Armstrong or not in Python. We will understand each line of code in detail.

So the first question comes to mind: what is an Armstrong number?

  • Any positive integer of n digits is Armstrong number, If the sum of the Nth power of each digit equals to the number itself.

I know it is a little bit confusing, but let's take it in a simple way with an example.

Example

  • Suppose we have a number 1634
    1634
  • The number of digits in 1634 is 4 i.e. 1,6,3 and 4
    1634 : 1 is the 1st digit, 6 is the 2nd digit, 3 is the 3rd digit and 4th digit is 4.
  • Next we'll calculate the Nth power of each digit.
  • In the above example, n is 4, so 14 + 64 + 34 + 44 ,we'll add all of them.
    pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n)
    Sum = 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4
  • If the sum equals the number itself, then it is the Armstrong number.
  • In above example, sum equals the number itself i.e. 1634, So the number is Armstrong.

Code Explanation

  1. First, we need an integer input from the user using the input method and convert it to integer type using Int method.
    number = int(input('Enter any positive number : '))
  2. Let's create a function which will take one parameter and return true if it is an Armstrong number otherwise false.
    def check_armstrong(num):
  3. Next step is to count the number of digits. We will convert input back to string using str() method and then find its length with length() method. Here order variable will have number of digits in the number
    order = len(str(num))
  4. Declare one variable sum, which will store our sum value, and let's set it to zero initially.
    sum = 0
  5. Calculate the power of N for each digit and add those numbers. We will be using a while loop which will continue till the number > 0.
    while num>0:​
     
    • calculate the last digit using the modulus operator
      digit = num % 10
    • calculate the Nth power and add those values to sum variable for every digit
      sum += digit ** order
    • divide the number to remove the last digit and to get the second last digit, third, last digit and so on until there is no number left.
      num = num // 10
    • loop will terminate once the number reaches zero.
  6. Next step is to compare the sum value with the num. But we have modified our num variable by dividing, so we need to preserve Original value before the loop by storing it in some other variable i.e. original.
    original = num
  7. If sum equals to original, then it is Armstrong number and will return true otherwise return false.
    if sum == original:
            return True
    return False
    • Else condition is not required above because if the condition matches, it will return true and exit the function otherwise will return false.
  8. If the number is between 1-9, both inclusive then the number is Armstrong. So we need to write one more If condition on the first line of the function.
    if num in range(1,10):
            return True
  9. Final step is to call this function with a number parameter. If it returns true, then we can print the message as “Number is Armstrong” else “The number is not Armstrong”.
    if check_armstrong(number):
        print('number is armstrong')
    else:
        print('number is not armstrong')

Source Code

number = int(input('Enter any positive number : '))

def check_armstrong(num):
    if num in range(1,10):
        return True

    order = len(str(num))
    sum = 0
    original = num
    while num>0:
        digit = num % 10
        sum += digit ** order
        num = num // 10
    if sum == original:
        return True
    return False

if check_armstrong(number):
    print('number is armsrong')
else:
    print('number is not armstrong')

Output

Enter any positive number : 1634
number is armstrong