Python program that finds all Armstrong numbers in a given range:

Created with Sketch.

Python program that finds all Armstrong numbers in a given range:

start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))

print("Armstrong numbers between", start, "and", end, "are:")

for number in range(start, end + 1):
    # Initialize variables
    original_number = number
    result = 0

    # Find the number of digits in the number
    number_of_digits = len(str(number))

    # Check if the number is Armstrong
    while number > 0:
        digit = number % 10
        result += digit ** number_of_digits
        number //= 10

    if original_number == result:
        print(original_number)

The program prompts the user to enter the start and end of the range using the input() function, and stores the values in the variables “start” and “end”. It then uses a for loop to iterate through the range of numbers between start and end (inclusive). For each number in the range, it uses the same logic as the previous example to check if the number is an Armstrong number, if yes, it prints the number.

You can also use the built-in python function pow() to raise the digit to the power of the number of digits and list comprehension to generate the list of digits in the number,

start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range

Leave a Reply

Your email address will not be published. Required fields are marked *