Program to check Armstrong number in Python

Program to check Armstrong number in Python. 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.

Program to check Armstrong number in Python

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)
n=int(input("Enter number:"))
#p=int(input("the number of digits"))
digits(n)
#armstrong(n)

Download Source code : For Download Click on the below Button

Step wise Explanation :

Step 1 : functions name 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()

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/test1.py 
Enter number:371
371 is an Armstrong number 
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Enter number:1634
1634 is an Armstrong number 
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
Enter number:561
561 is not an Armstrong number
>>> 

Output Explanation :

Step 1: Number to be checked is input.
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 Program to check Armstrong number in Python post.This program requires the knowledge of Armstrong number, input capture, while loop and if else loop. 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.