Armstrong Number In Python


In this article, we are going to look at the Armstrong number in Python. We will learn about what is Armstrong numbers, then will write a program to find if a number is an Armstrong number or not, find the Nth Armstrong number, etc.

    Table Of Contents

  1. What Is Armstrong Number?
  2. Program To Check Armstrong Number
  3. Find Nth Armstrong Number In Python
  4. Python Program To Find Armstrong Number In An Interval
  5. Conclusion

Armstrong number in python

What Is Armstrong Number?

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, the number 153 is an Armstrong number since 13 + 53 + 33 = 153

Armstrong number is also known as the narcissistic number, plusperfect digital invariant (PPDI), or plus perfect number.

Another example is the number 371, since 33 + 73 + 13 = 371.

The first few Armstrong numbers are: 1, 2, 3, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774, ...


Python Program To Check Armstrong Number

Let's create a Python program to check if a number is an Armstrong number or not.

The program will ask the user to enter a number and then check if the number is an Armstrong number or not.

Here is the program for the Armstrong number in Python.

# function to calculate the number of digits in a number
def findOrder(num):
    count = 0
    while num > 0:
        if num % 10 >= 0:
            count += 1
            num = num // 10
    return count

# function to check if a number is Armstrong or not
def isArmstrong(num):
    # create a variable and store the same value as the num
    testNum = num
    sum = 0
    # find the number of digits in the number
    n = findOrder(num)

    while testNum > 0:
        sum += (testNum % 10) **n
        testNum = testNum // 10
    if sum == num:
        print(num, "is an Armstrong number")
        return True
    else:
        print(num, "is not an Armstrong number")
        return False

isArmstrong(153)
isArmstrong(220)
isArmstrong(371)
isArmstrong(407)

Output:

153 is an Armstrong number
220 is not an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Code Explanation:

  1. First, we will create a function findOrder() to calculate the number of digits in a number.
  2. The findOrder() function executes and loop that checks if num % 10 is greater than 0 if yes then increase the counter by 1 and divide the number by 10. It does it until the number becomes 0.
  3. Then, we will create a function isArmstrong() to check if a number is Armstrong or not.
  4. isArmstrong() function will take the number then calculate the number of digits in it using findOrder() function and run a loop.
  5. The loop will take every digit of the number, raise it to its order, add it to the sum and finally divide the number by 10.
  6. At last, check if the sum is equal to the number, if yes print the output and return true.

Another approach of finding the order of number

We can also calculate the number of digits in the number by converting it to a string and checking its length by the len() function.

For next use convert it again to a number by using the int() function.

Here is the program.

def isArmstrong(num):
    # find the number of digits in the number
    digits = len(str(num))
    
    # convert num to integer
    num = int(num)
    # find sum of digits
    # execute while loop
    sum = 0
    checkNum = num
    while (checkNum > 0):
        sum += (checkNum%10)**digits
        checkNum = int(checkNum/10)
        # print("sum=", sum)
    if(num==sum):
        print(f"{num} is an Armstrong number")
        return True
    else:
        print(f"{num} is not an Armstrong number")
        return False

# call function
isArmstrong(153)
isArmstrong(258)
isArmstrong(8208)
153 is an Armstrong number
258 is not an Armstrong number
8208 is an Armstrong number

Find Nth Armstrong Number In Python

Above we have created a program to tell if a number is Armstrong or not. Let's now create a program that calculates the Nth Armstrong number.

Although one thing is to note that the higher the value of N, the more time it will take to find the Armstrong number.

def nth_Armstrong(n):
    startNum = 1
    count = 0
    while count < n:
        # check the number of digits in the number
        digits = len(str(startNum))

        # finding sum
        sum = 0
        checknum = startNum
        while checknum > 0:
            sum += (checknum%10)**digits
            checknum = int(checknum/10)
        if(sum == startNum):
            count += 1
        startNum += 1
    
    print(f"{n}th Armstrong number is {startNum-1}")
    return startNum - 1

nth_Armstrong(10)
nth_Armstrong(15)

Output:

10th Armstrong number is 153
15th Armstrong number is 8208

Code Explanation:

  1. The nth_Armstrong() function is defined to find the Nth Armstrong number.
  2. Initialize the starting number from where you want to count, we take 1 as the starting number.
  3. Initialize the count variable to 0 to count the number of Armstrong numbers we have found.
  4. Execute a python while loop and check if the count is less than the value of N if yes, then calculate the sum of each digit raised to the power of the length of the number.
  5. If the sum is equal to the number, then the number is an Armstrong number.
  6. Increase the count by 1.
  7. Increase the starting number by 1 to check the next number.
  8. If the count is equal to the value of N, then print the Nth Armstrong number.

Python Program To Find Armstrong Number In An Interval

We can easily find the Armstrong numbers in an interval using Python.

Here is the complete code for the Armstrong number in Python in an interval.

def range_Armstrong(start, end):
    armstrong = []
    for num in range(start, end + 1):
        # find digits in number
        digits = len(str(num))

        # finding sum
        sum = 0
        checknum = num
        while checknum > 0:
            sum += (checknum%10)**digits
            checknum = int(checknum/10)
        if(sum == num):
            armstrong.append(num)
    
    print(armstrong)
    return armstrong

range_Armstrong(100, 1000)
range_Armstrong(500, 9000)

Output:

[153, 370, 371, 407]
[1634, 8208]

Code Explanation:

  1. Create a function that takes 2 parameters, start, and end, and returns the list of Armstrong numbers in the interval.
  2. Initialize the Armstrong list variable to store the Armstrong numbers.
  3. Execute a for loop within range of given interval.
  4. For each number in the interval, find the number of digits in the number and then check if the sum of each digit raised to the power of the length of the number is equal to the number.
  5. If yes, then the number is Armstrong number.
  6. Append the number to the Armstrong list.
  7. Print the Armstrong variable.

Conclusion

Armstrong numbers are very fascinating numbers. We created programs to find Nth Armstrong number and another program to tell if a number is Armstrong or not.

We have covered these ideas with complete code in this article.

Also, learn about the Fibonacci series in Python.

Reference: Wikipedia