Python Program to Find Armstrong Number between an Interval

In this tutorial, we are going to learn how to find Armstrong Number between an interval in Python. It is very simple if we know what is Armstrong Number. Let’s see

Find Armstrong Number between an Interval in Python

Armstrong Number can be defined as the number is equal to the sum of Nth power of each digit in that number. Where N is number of digits in that number.

abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....

For example: 153    1*1*1 + 5*5*5 + 3*3*3= 153   so it is Armstrong Number.

16    1*1 + 6*6 = 37   so it is not an Armstrong Number.

x=int(input("lower limit: "))
y=int(input("upper limit: "))
print("Armstrong Numbers are: ")
for Number in range(x,y):
 digits=0
 temp=Number
 while temp>0: # no of digits
  digits=digits+1
  temp=temp//10
 sum=0
 temp=Number
 while temp>0: # calculate armstrong number
  last_digit=temp%10
  sum=sum+(last_digit**digits)
  temp=temp//10
 if Number == sum:
    print(Number)

Code Explanation:

User has to enter two values lower limit and upper limit. Loop traversal from the lower limit to the upper limit.

Count the Number of individual digits and divide the given number into individual digits.

Calculate the power of n for each individual and add those numbers.

Compare the original value with Sum value. If they are equal then print the Number and it is Armstrong number.


Output:-

lower limit: 100
upper limit: 200
Armstrong Numbers are: 
153

So Guy’s, I hope you really enjoy this tutorial and feel free to leave a comment if you have any doubt.

You may also learn:

Leave a Reply

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