Python Program ArmStrong Number between two numbers

In this example, We will learn to generate Armstrong numbers between two numbers. This program uses the same function to determine to check Armstrong number. The source code of the function to check Armstrong number is

def armstrong(n):
    m = n
    sum = 0
    while(n != 0):
        rem = n % 10
        sum += rem**3
        n = n//10
    return True if sum == m else False

Now the main function to call this function as well as accept tow range values

if __name__ == "__main__":
    n1 = int(input('Enter first value  :'))
    n2 = int(input('Enter last value  :'))
    for x in range(n1, n2+1):
        if(armstrong(x)):
            print(x)

Thus the whole program looks like this

def armstrong(n):
    m = n
    sum = 0
    while(n != 0):
        rem = n % 10
        sum += rem**3
        n = n//10
    return True if sum == m else False


if __name__ == "__main__":
    n1 = int(input('Enter first value  :'))
    n2 = int(input('Enter last value  :'))
    for x in range(n1, n2+1):
        if(armstrong(x)):
            print(x)

Output

Enter first value  :100
Enter last value  :500
153
370
371
407

If you are able to generate any better solution for the above mentioned problem. Please let us know so that we can publish your version.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.