Python Program to Find Armstrong Numbers in an Interval

Introduction

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. This tutorial will guide you through creating a Python program that finds all Armstrong numbers within a specified interval.

Example:

  • Input: start = 100, end = 500
  • Output: Armstrong numbers between 100 and 500 are: 153, 370, 371, 407

Problem Statement

Create a Python program that:

  • Takes two numbers as input, representing the lower and upper bounds of an interval.
  • Finds all Armstrong numbers within this interval.
  • Displays the Armstrong numbers.

Solution Steps

  1. Take Input for the Interval: Use the input() function to get the start and end of the interval from the user.
  2. Convert Input to Integers: Convert the input strings to integers using int().
  3. Check Each Number in the Interval: For each number in the range, check if it is an Armstrong number.
  4. Display the Armstrong Numbers: Collect and display all Armstrong numbers found within the interval.

Python Program

# Python Program to Find Armstrong Numbers in an Interval # Author: https://www.rameshfadatare.com/ # Step 1: Take input for the interval start = int(input("Enter the start of the interval: ")) end = int(input("Enter the end of the interval: ")) # Step 2: Check each number in the interval print(f"Armstrong numbers between {start} and {end} are:") for num in range(start, end + 1): # Calculate the number of digits num_digits = len(str(num)) # Calculate the sum of each digit raised to the power of the number of digits sum_of_powers = sum(int(digit) ** num_digits for digit in str(num)) # Step 3: Check if the sum equals the original number if sum_of_powers == num: print(num, end=" ") print() # For a newline after printing all Armstrong numbers 

Explanation

Step 1: Take Input for the Interval

  • The input() function prompts the user to enter the start and end of the interval. The inputs are converted to integers using int().

Step 2: Check Each Number in the Interval

  • The program iterates over each number in the specified range. For each number, it calculates the number of digits and then checks if the number is an Armstrong number by summing the digits raised to the power of the number of digits.

Step 3: Display the Armstrong Numbers

  • The print() function is used to display all Armstrong numbers found within the interval. The numbers are printed on the same line, separated by spaces.

Output Example

Example:

Enter the start of the interval: 100 Enter the end of the interval: 500 Armstrong numbers between 100 and 500 are: 153 370 371 407 

Example:

Enter the start of the interval: 1 Enter the end of the interval: 1000 Armstrong numbers between 1 and 1000 are: 1 153 370 371 407 

Conclusion

This Python program demonstrates how to find all Armstrong numbers within a specified interval. It effectively combines loops, conditionals, and mathematical operations, making it a valuable example for beginners learning about Armstrong numbers and control structures in Python.

Leave a Comment

Scroll to Top