In this python program, we will ask the user to enter a number and as an output, the program will tell if the entered number is an Armstrong number or not.
What is Armstrong Number?
A number is said to be Armstrong's number if the sum of that number each digit which is raised to the power of total digits present in that number is equal to the number itself. for instance: 153 is an Armstrong number because: 153 = (1) 3 + (5) 3 +(3) 3
Prerequisite Topics Armstrong Number in Python
- Python Input, Output
- Python Data types
- Python Type Conversion
- Python loop
- Python Arithmetic and comparisons Operators
- First, we will ask the user to enter a number
- now using the len() function we compute the total digit present in the entered number.
- now using the for loop we will go through each digit present in the number and raise it to the power of the total length of the number, and simultaneously we will also compute its sum.
Armstrong Number in Python Program
n = input("Enter a number: ") # n is a string not a number
power = len(n)
sum=0
#loop through a string
for i in n:
sum += int(i)**power
if sum == int(n):
print(n + " is an Armstrong Number")
else:
print(n+" is not an Armstrong Number")
Output 1:
Enter a number: 1634
1634 is an Armstrong Number
Output 2:
Enter a number: 23
23 is not an Armstrong Number
People are also reading:
- Python Program to Merge Mails
- Python Program to Sort Words in Alphabetic Order
- Python Program to Convert Decimal to Binary Using Recursion
- Python Program to Find Sum of Natural Numbers Using Recursion
- Python Program to Check Whether a String is Palindrome or Not
- Python Program to Check if a Number is Odd or Even
- Python Program to Convert Celsius To Fahrenheit
- Python Program to Swap Two Variables
- How to Find Square Root in Python?
- How many Centimeters in a Foot through Python?
Leave a Comment on this Post