Python Program - Find Armstrong Number in an Interval
here's a Python program that finds Armstrong numbers in a given interval:
# function to check if a number is an Armstrong number
def is_armstrong(number):
# convert the number to a string to get its length
num_str = str(number)
num_len = len(num_str)
# initialize sum variable to 0
sum = 0
# loop through each digit in the number
for digit in num_str:
# raise the digit to the power of the number of digits
sum += int(digit) ** num_len
# check if the sum is equal to the original number
return sum == number
# define the interval to search for Armstrong numbers
start = 100
end = 1000
# loop through each number in the interval and check if it's an Armstrong number
for number in range(start, end+1):
if is_armstrong(number):
print(number)
In this program, the is_armstrong() function checks if a given number is an Armstrong number. It first converts the number to a string to get its length, and then loops through each digit in the number, raising each digit to the power of the number of digits and adding them up. Finally, it checks if the sum is equal to the original number, and returns True or False accordingly.
In the main part of the program, we define the interval to search for Armstrong numbers (in this case, from 100 to 1000), and then loop through each number in the interval using a for loop. For each number, we call the is_armstrong() function to check if it's an Armstrong number, and if it is, we print it out.
