Here in this article, we have provided a Python program that asks the user to enter the lower and upper limit numbers, and the program computes all the Armstrong numbers present in between those two numbers. Before discussing the Python code to find Armstrong Number in an interval, let's know a little more about the concept of Armstrong Numbers.
Armstrong Number
A number is said to be an Armstrong Number if the sum of each digit of the number 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 to Create the Python Program
- Python Input, Output
- Python Data types
- Type Conversion in Python
- Python for and while loop
- Python nested loops
- Arithmetic and Comparison Operators in Python
- First, ask the user to enter the lower and the upper limits.
- Now, using the Python for loop , we will go through each value from lower to the upper limit.
- Next, inside the for loop, we will create a nested while loop that will compute the Armstrong value for the number.
- The program will check if the number given by the for loop is equal to the Armstrong value computed by the while loop. At last, print all the Armstrong Numbers found.
A Python Program to Find Armstrong Number in an Interval
Python Code:
lower = int(input("Enter the Lower limit: "))
upper = int(input("Enter the upper limit: "))
#this for loop is to traverse through the each value from lower to upper
for i in range(lower, upper + 1):
# power of number
power = len(str(i))
sum = 0
temp = i
#this while loop will calculate the armstrong value of i
while temp > 0:
digit = temp % 10
sum += digit ** power
temp //= 10
if i == sum:
print(i)
Output 1:
Enter the Lower limit: 1
Enter the upper limit: 1000
1
2
3
4
5
6
7
8
9
153
370
371
407
Output 2:
Enter the Lower limit: 1
Enter the upper limit: 100
1
2
3
4
5
6
7
8
9
Conclusion
Now you should know how to find Armstrong Number in an interval using Python. You can try to write the same program in different programming languages, such as C, C++, Java, and Kotlin.
People are also reading:
- WAP to Find Cube of a Number using Functions
- Python Program To Display Powers of 2 Using Anonymous Function
- WAP to Print the Following Triangle
- Python Program to Convert Decimal to Binary, Octal and Hexadecimal
- How to Find Square Root in Python?
- Python Program to Find HCF or GCD
- How to Check if a Python String Contains Another String?
- Python Examples
- How many Centimeters in a Foot through Python?
- Rectangle & Pyramids Pattern in C++
Leave a Comment on this Post