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
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")
Enter a Number:371
371 is an Armstrong number
Algorithm:
- 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 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)
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")
Enter a Number:656
656 is not an Armstrong number