Python Program to Check Armstrong Number using while loop

    In this program, we will make a Python Program to Check Armstrong Number using while loop and for loop. We will also make a python program to print(or find) Armstrong numbers in an interval.

    Armstrong numbers in python: Armstrong numbers is a number that is the sum of its own digits each raised to the power of the number of digits.

    Python Program to Check Armstrong Number using while loop

    Python Program to Check Armstrong Number using while loop

    In this Python program, we are going to check whether the number entered by the user is an Armstrong number or not.

    num = int(input("Enter a Number:"))
    order = len(str(num))
    temp = num;
    sum = 0
    while(temp>0):
    	digit =temp%10
    	sum += digit **order
    	temp = temp//10
    if(sum==num):
    	print("",num,"is an Armstrong number")
    else:
    	print("",num,"is not an Armstrong number")
    
    	

    Output:
    Enter a Number:371
    371 is an Armstrong number

    Algorithm:

    1. Input the number from the user.
    2. Initialize "order" with the length of the num variable.(order= Number of digits)
    3. Store the value of the num variable in the temp variable.
    4. Initialize the sum of digits with zero.
    5. While temp>0 repeat steps 6-7
    6. digit =temp%10 and sum += digit **order
    7. temp = temp//10
    8. If the sum equals to num, then we will print the number entered by the user is an Armstrong number

    Related post: Menu-Driven Program in Python using while loop

    Python Program to find Armstrong Number in an Interval

    In this program, we will print Armstrong numbers between any interval according to the lower and upper range. Here, the lower range and upper range are given by the user.

    lower = int(input("Enter lower range:"))
    upper = int(input("Enter upper range:"))
    
    
    for num in range(lower,upper+1):
    	sum = 0
    	order = len(str(num))
    	temp = num;
    	while(temp>0):
    		digit=temp%10
    		sum += digit **order
    		temp = temp//10
    		if(sum==num):
    			print(num)
    

    Output: Enter lower range:100
    Enter upper range:1000
    125
    153
    216
    370
    371
    407
    729

    Program to Check Armstrong Number using for loop

    num = int(input("Enter a Number:"))
    order = len(str(num))
    temp = num;
    sum = 0
    stnum=str(num)
    for i in stnum:
        digit =temp%10
        sum += digit **order
        temp = temp//10
    if(sum==num):
        print("",num,"is an Armstrong number")
    else:
        print("",num,"is not an Armstrong number")
    

    Output:
    Enter a Number:656
    656 is not an Armstrong number

    About this Program to Check Armstrong Number using while loop:

    This post is all about making a program to check an Armstrong number. I hope this program will run successfully on your device. If you have any doubts related to this program, you should comment below.
    Siddharth Jha
    • Hackerrank 5* • Full Stack Developer • Masters in SEO

    Related Posts

    Post a Comment