An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.
Consider 153. It has 3 digits, so we raise each digit to the power of 3(the number of digits) and sum them up:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Checking Armstrong Number using for loop in Python
n = str(int(input("Enter an integer: "))) digit_sum = 0 for i in n: digit_sum = digit_sum + int(i)**len(n) if int(n) == digit_sum: print(n, "is an Armstrong number") else: print(n, "is not an Armstrong number")
Output:
Also read:
- Python | Asking the user for input until they give a valid response
- Python | How to iterate through two lists in parallel?
- Python | How to sort a dictionary by value?
- Python | Remove items from a list while iterating
- Python | How to get dictionary keys as a list
- How to represent Enum in Python?
- 5 methods | Flatten a list of lists | Convert nested list into a single list in Python
- What does if __name__ == __main__ do in Python?
- Python | CRUD operations in MongoDB
- Create your own ChatGPT with Python
- Filter List in Python | 10 methods
- Radha Krishna using Python Turtle
- Yield Keyword in Python
- Python Programming Examples | Fundamental Programs in Python
- Python | Delete object of a class
- Python | Modify properties of objects
- Python classmethod
- Python | Create a class that takes 2 parameters, print both parameters
- Python | Create a class, create an object of the class, access, and print property value
- Python | Calculator using lambda
- Python | Multiply numbers using lambda
- Python | Print Namaste using lambda
- Iterate over a string in Python
- Python | join() | Join list of strings
- Python | isalnum() method | check if a string consists only of alphabetical characters or numerical digits
- Python | isupper() and islower() methods to check if a string is in all uppercase or lowercase letters
- Python | count substring in a String | count() method
- Python | enumerate() on String | Get index and element
- Python | Convert a String to list using list() method
- Euler’s Number in Python