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
Recommended:-Python Program to Calculate Simple Interest
Recommended:-Python Program to Find Average of n Numbers
Recommended:-Python Program to ASCII Value of Character
Recommended:-Python Program to Calculate Compound Interest
Recommended:-Python Program to Check Leap Year
Recommended:-Python Program To Print Number Pattern
Recommended:-Python Program to Print Star Pattern
Recommended:-Python Program Calculate Sum of n Numbers
Recommended:-Python Program to Reverse Numbers
Recommended:-Python Program to Find Largest of n Numbers
Recommended:-Python Program to Find Power of Number
Recommended:-Python Program to Print Prime Number From 1 to N
Recommended:-Python Program to Find Square Root of Number
Recommended:-Python Program to Find Cube of Number
Recommended:-Python Palindrome Program
Recommended:-Python Program to Count Set Bits in a Number
Recommended:-Python Program for BMI Calculation
Recommended:-Python Program to Find Out Absolute Value
Recommended:-Python Random Number Generator Code
Recommended:-Python program for Zip, Zap and Zoom game
Recommended:-Python Program to Swap Two Numbers