Python Program to find Armstrong number in an interval


Python Program to find Armstrong number in an interval. This program requires the knowledge of Armstrong number, input capture, while loop and if else condition. This program is must for Beginners, Class 11, 12 and B Tech Computer Science students.

Python Program to find Armstrong number in an interval

What is Armstrong number ?

 A number is thought of as an Armstrong number if the sum of its own digits raised to the power number of digits gives the number itself. For example, 0, 1, 153, 370, 371, 407 are three-digit Armstrong numbers and, 1634, 8208, 9474 are four-digit Armstrong numbers and there are many more.

Source code :

def armstrong(n,c):
    count=0
    temp=n
    
    while temp>0 :
        digit= temp % 10
        count += digit ** c
        temp //=10
        #print(count)
        
        #print(c)
    if n == count:
        print(n, "is an Armstrong number ")
    #else:
        #print(n,"is not an Armstrong number")
def digits(n):
    count=0
    temp=n
    c=0
    while temp>0 :
        digit= temp % 10
        #count += digit ** p
        temp //=10
        c+=1
        #print(c)
    #if n == count:
     #   print(n, "is an Armstrong number ")
    #else:
     #   print(n,"is not an Armstrong number")
    armstrong(n,c)

n1=int(input("Enter number:start"))
n2=int(input("Enter number:end"))
#p=int(input("the number of digits"))
n=0
while  n1<n2 :
    digits(n1)
    n1=n1 +1
    if n1>n2:
        break
#armstrong(n)



Download Source code : For Download Click on the below Button

Step wise Explanation :

Step 1 : functions named digits and armstrong is to be defined
def armstrong(n):
def digits(n):
step 2:Intial value are defined for count and temp variable in both
count=0
temp=n
step 3:While loop is applied in both function
while temp>0 :
digit= temp % 10
count += digit ** 3
temp //=10
Step 4 : IF and else statement is applied in armstrong n.o
if n == count:
print(n, “is an Armstrong number “)
else:
print(n,”is not an Armstrong number”)
n=int(input(“Enter number:”))
armstrong(n)
Step 5 : Armstrong n.o function is called from digits ()
Step 6: Input Interval from min to max and then the digit function is given value in a while loop

Output:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/armstrong in interval.py 
Enter number:start0
Enter number:end20000
0 is an Armstrong number 
1 is an Armstrong number 
2 is an Armstrong number 
3 is an Armstrong number 
4 is an Armstrong number 
5 is an Armstrong number 
6 is an Armstrong number 
7 is an Armstrong number 
8 is an Armstrong number 
9 is an Armstrong number 
153 is an Armstrong number 
370 is an Armstrong number 
371 is an Armstrong number 
407 is an Armstrong number 
1634 is an Armstrong number 
8208 is an Armstrong number 
9474 is an Armstrong number 
>>> 

Output Explanation :

Step 1: Number range to be checked is input as number 1 and number 2
Step 2: Mathematical calculation takes place, while loop and if and else statements are evaluated.
Step 3: Output is shown.

Thank you for reading and learning from Python Program to find Armstrong number in an interval post.This program requires the knowledge of Armstrong number, input capture, while loop and if else condition. This program is must for Beginners, Class 11, 12 and B Tech Computer Science students. For more basic program and practical file of 11 and 12 class link is given below.

Python Programs Archives – CS Study

Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study

Computer Science Practical file for class 12 Term-2 – CS Study

Leave a Comment

Your email address will not be published.