Print Armstrong Numbers Between Two Integers in Python

Mathematics and programming when combined make a very deadly combination. As the ability to solve complex mathematical questions in itself is a great deal. But when one has to do the same thing by writing up a code, things get somewhat more complicated. Not to mention the language your coding in also determines whether it’s going to be easy to difficult. Well, Armstrong numbers are very well known in the mathematical world. Therefore today we’re going to write a program to print Armstrong Numbers Between Two Integers in Python.


What are Armstrong Numbers?


  • Let us consider Number X with N being the no digits in X. Then X will be said as the Armstrong number if the sum of each digit of X is raised with order N equals to X itself.


  • Eg:  153 is the Armstrong Number.
  • Therefore, 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 = 153


What’s The Approach?


  • We’ll iterate a for loop where Low and High will determine the range of integers we’ll have to traverse.


  • For each integer in the range, we will Find The Number Of Digits (N) so that we can determine the order.


  • Once we find the order, then for each digit (R) we will compute  RN After that, we will perform the addition of the computed values.


  • If the computed addition equals the iterative variable i of the for loop we’ll print it, otherwise, we’ll pass.


Also Read: Create A Stack Using Array in Python


Python Program To Print Armstrong Numbers Between Two Integers



num1 = 100
num2 = 400

153 370 371


# PYTHON program to find Armstrong
# numbers in a range
import math

# Prints Armstrong Numbers in given range
def findArmstrong(low, high) :
    for i in range(low + 1, high) :
        # number of digits calculation
        x = i
        n = 0
        while (x != 0) :
            x = x / 10
            n = n + 1
        # compute sum of nth power of
        pow_sum = 0
        x = i
        while (x != 0) :
            digit = x % 10
            pow_sum = pow_sum + math.pow(digit, n)
            x = x / 10
        # checks if number i is equal to
        # the sum of nth power of its digits
        if (pow_sum == i) :
            print(str(i) + " "),

# Driver code
num1 = 100
num2 = 400
findArmstrong(num1, num2)



I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *