Python Program to Find Armstrong Numbers in an Interval
|This Python example will cover the Program to Find Armstrong Numbers in an Interval.
1. What is an Armstrong Number?
An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits raised to the power of the number of digits.
In other words, it is a number where the sum of its digits, each raised to the power of the number of digits, is equal to the original number.
Example: abcd... = an + bn + cn + dn + ... The number 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153
Some of the topics which will be helpful for understanding the program implementation better are:
Let’s go through each of the implementation methods one by one.
2. Python Program to find Armstrong Numbers within an interval
This program can be run in any Python environment, such as the Python IDLE, and it will prompt the user for the start and end of the interval. It will then print all the Armstrong numbers within that interval.
# Python Program to find Armstring numbers in a range def is_armstrong(num): n = len(str(num)) temp = num sum = 0 while temp > 0: digit = temp % 10 sum += digit ** n temp //= 10 return sum == num def find_armstrong_in_interval(start, end): for num in range(start, end + 1): if is_armstrong(num): print(num) start = int(input("Enter start of interval: ")) end = int(input("Enter end of interval: ")) print("Armstrong numbers in given interval:") find_armstrong_in_interval(start, end)
Output Enter start of interval: 0 Enter end of interval: 200 Armstrong numbers in given interval: 0 1 2 3 4 5 6 7 8 9 153
3. Conclusion
In conclusion, this Python program is a simple example of how to find Armstrong numbers within a given interval. It demonstrates how to use functions and loops in Python, and how to implement the logic for finding Armstrong numbers.
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?