Python Program to Check Armstrong Number

Python program to check armstrong number; Through this tutorial, i will show you how to check armstrong number using functions, while loop, for loop in python.

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Let’s try to understand why 153 is an Armstrong number.

Python Programs to Check Armstrong Number

See the following python programs to check armstrong number using for, while loop and function; as shown below:

  • Python program to find armstrong number using while loop
  • Python program to find armstrong number using For loop
  • Python program to find armstrong number in an interval

Python program to find armstrong number using while loop

  • Get input the number from the user in python program.
  • Initialize “order” with the length of the num variable.(order= Number of digits)
  • Store the value of the num variable in the temp variable.
  • Initialize the sum of digits with zero.
  • While temp>0 repeat steps 6-7
  • digit =temp%10 and sum += digit **order
  • temp = temp//10
  • If the sum equals to num, then print 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")

After executing the python program, the output will be:

Enter a Number:371
371 is an Armstrong number

Python program to find armstrong number using For loop

  • Get input the number from the user in python program.
  • Initialize “order” with the length of the num variable.(order= Number of digits)
  • Store the value of the num variable in the temp variable.
  • Initialize the sum of digits with zero.
  • While temp>0 repeat steps 6-7
  • digit =temp%10 and sum += digit **order
  • temp = temp//10
  • If the sum equals to number and print the number entered by the user is an Armstrong number
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")

After executing the python program, the output will be:

Enter a Number:656
656 is not an Armstrong number

Python program to find armstrong number in an interval

  • Get input the number from the user in python program.
  • Initialize “order” with the length of the num variable.(order= Number of digits)
  • Store the value of the num variable in the temp variable.
  • Initialize the sum of digits with zero.
  • While temp>0 repeat steps 6-7
  • digit =temp%10 and sum += digit **order
  • temp = temp//10
  • If the sum equals to number and print the number entered by the user is an Armstrong number
# Program to check Armstrong numbers in a certain interval
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
for num in range(lower, upper + 1):
   # order of number
   order = len(str(num))
    
   # initialize sum
   sum = 0
   temp = num
   while temp > 0:
       digit = temp % 10
       sum += digit ** order
       temp //= 10
   if num == sum:
       print(num)

After executing the python program, the output will be:

Enter a number: 663
663 is not an Armstrong number

Recommended Python Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.