Python Program to Check Armstrong Number using while loop

We will make a Python Program to Check Armstrong Number using while loop, for loop and Python Program to find Armstrong Number in an interval.
Table of Contents

In this program, we will make a program to check whether the given number is Armstrong. We will also make a python program to print(or find) Armstrong numbers in an interval.

Armstrong numbers in python: Armstrong number is a number that is the sum of its 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 will check whether the number entered by the user is an Armstrong number.

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 num, then we will print the number entered by the user as an Armstrong number

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

Python Program to find Armstrong Number in an Interval.

This program will print Armstrong numbers between intervals 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

Armstrong Number in Python 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

Conclusion:

This post is all about making a program to check an Armstrong number. I hope this program will run successfully on your device. You should comment below if you have any doubts about this program.

Post a Comment

Please do not enter any spam link in the comment box.