Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython Program to Check Armstrong Number

Python Program to Check Armstrong Number

Program to check Armstrong number in python; In this tutorial, you will learn how to check armstrong numbers 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

  • 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

Follow the below steps and write a python program to find armstrong number using while loop:

  • Take input the number from the user.
  • 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 we will 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

Follow the below steps and write python program to find armstrong number using For loop:

  • Take input the number from the user.
  • 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 we will 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

Follow the below steps and write python program to find armstrong number in an interval:

  • Take two input number from the user.
  • 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 we will 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 Programs

RELATED ARTICLES

Most Popular

Recent Comments