Python Program find Armstrong Numbers within a given Interval

Python Program find Armstrong Numbers within a given Interval
Reading Time: 8 minutes

Introduction:

The provided Python program is designed to find Armstrong numbers within a given interval specified by the user. An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. This program prompts the user to input the start and end values of the interval, and then it searches for and lists all the Armstrong numbers within that interval. An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Algorithm:

  1. Define a function is_armstrong_number(num) that takes an integer num as input and returns True if it is an Armstrong number, otherwise False. The function first converts the number to a string and calculates the number of digits. It then calculates the sum of each digit raised to the power of the number of digits, and compares it to the original number. If they are equal, it returns True, indicating that the number is an Armstrong number.
  2. Define another function find_armstrong_numbers_in_interval(start, end) that takes two integers start and end as input and returns a list of Armstrong numbers found in the specified interval. It iterates through each number in the range from start to end + 1, and for each number, it calls the is_armstrong_number function to check if it is an Armstrong number. If it is, the number is added to the list of Armstrong numbers.
  3. In the __main__ section, the program first prompts the user to input the start and end values of the interval. It then performs input validation to ensure that the start value is less than or equal to the end value. If the input is valid, the program calls the find_armstrong_numbers_in_interval function to find the Armstrong numbers in the given interval and stores them in a list. Finally, it prints the list of Armstrong numbers if any were found, or a message indicating that no Armstrong numbers were found.

Python Code for Finding Armstrong Number:

    
     def is_armstrong_number(num):
    num_str = str(num)
    num_digits = len(num_str)
    sum_of_digits = sum(int(digit) ** num_digits for digit in num_str)
    return sum_of_digits == num
def find_armstrong_numbers_in_interval(start, end):
    armstrong_numbers = []
    for num in range(start, end + 1):
        if is_armstrong_number(num):
            armstrong_numbers.append(num)
    return armstrong_numbers
if __name__ == "__main__":
    try:
        start = int(input("Enter the start of the interval: "))
        end = int(input("Enter the end of the interval: "))
        if start > end:
            print("Invalid interval. The start value should be less than or equal to the end value.")
        else:
            armstrong_numbers = find_armstrong_numbers_in_interval(start, end)
            if armstrong_numbers:
                print("Armstrong numbers in the interval:", armstrong_numbers)
            else:
                print("No Armstrong numbers found in the interval.")
    except ValueError:
        print("Invalid input. Please enter valid integers for the interval.")


    
   

Input :

    
     Enter the start of the interval: 100
Enter the end of the interval: 500
    
   

Output :

    
     Armstrong numbers in the interval: [153, 370, 371, 407]
    
   

Explanation:

  1. The is_armstrong_number(num) function is defined to determine if a given number is an Armstrong number. It converts the number to a string to count the number of digits and then calculates the sum of each digit raised to the power of the number of digits. If the sum is equal to the original number, it returns True, indicating that the number is an Armstrong number.
  2. The find_armstrong_numbers_in_interval(start, end) function takes the start and end values of the interval and iterates through each number in the range from start to end + 1. For each number in the range, it calls the is_armstrong_number function to check if it is an Armstrong number. If it is, the number is added to the list of Armstrong numbers.
  3. The program prompts the user to input the start and end values of the interval and performs input validation to ensure the start value is less than or equal to the end value. If the input is valid, it calls the find_armstrong_numbers_in_interval function to find Armstrong numbers in the given interval. If any Armstrong numbers are found, it prints the list of Armstrong numbers; otherwise, it prints a message indicating that no Armstrong numbers were found.