Write a Python program to find Armstrong number in an interval
An Armstrong number is a number that is equal to the sum of the cubes of its digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
Here is a Python program to find Armstrong numbers in an interval:
def is_armstrong_number(num):
"""Returns True if the given number is an Armstrong number, False otherwise."""
sum_of_cubes = 0
temp = num
while temp > 0:
digit = temp % 10
sum_of_cubes += digit ** 3
temp //= 10
return num == sum_of_cubes
def find_armstrong_numbers_in_interval(lower_bound, upper_bound):
"""Returns a list of Armstrong numbers in the given interval."""
armstrong_numbers = []
for num in range(lower_bound, upper_bound + 1):
if is_armstrong_number(num):
armstrong_numbers.append(num)
return armstrong_numbers
# Example usage:
lower_bound = 100
upper_bound = 200
armstrong_numbers = find_armstrong_numbers_in_interval(lower_bound, upper_bound)
print("Armstrong numbers in the interval [", lower_bound, ", ", upper_bound, "] are:")
for armstrong_number in armstrong_numbers:
print(armstrong_number)
Output:
Armstrong numbers in the interval [100, 200] are:
153
370
371
This program works by first defining a function called is_armstrong_number()
. This function takes a number as input and returns True if the number is an Armstrong number, False otherwise.
The program then defines another function called find_armstrong_numbers_in_interval()
. This function takes two numbers as input: the lower bound of the interval and the upper bound of the interval. The function returns a list of Armstrong numbers in the given interval.
The program then uses the find_armstrong_numbers_in_interval()
function to find all of the Armstrong numbers in the interval [lower_bound, upper_bound]
. The program then prints a list of all of the Armstrong numbers to the console.
This program can be modified to find Armstrong numbers in any interval. The user can simply change the values of the lower_bound
and upper_bound
variables.
0 Comments