Program to Find Armstrong Number in an Interval

Python Program to Find Armstrong Number in an Interval

In the previous article, we have discussed Python Program To Calculate the Angle Between Two Vectors
Armstrong Number:

Beginners sometimes ask what the Armstrong number, also known as the narcissist number, is. Because of the way the number behaves in a given number base, it is particularly interesting to new programmers and those learning a new programming language. The Armstrong number meaning in numerical number theory is the number in any given number base that forms the sum of the same number when each of its digits is raised to the power of the number’s digits.

Ex: 153, 371 etc.

Explanation:

Here 1^3 + 5^3 + 3^3 = 153  so it is Armstrong Number

Example1:

Input:

Given lower limit =  50
Given upper limit =  1800

Output:

The Armstrong numbers in the given range: 
153
370
371
407
1634

Example 2:

Input:

Given lower limit =  500
Given upper limit =  2000

Output:

The Armstrong numbers in the given range: 
1634

Program to Find Armstrong Number in an Interval

Below are the ways to Find Armstrong numbers in a given interval.

Method #1: Using For Loop (Static input)

Approach:

  1. Give the lower limit range as static input and store it in a variable.
  2. Give the upper limit range as static input and store it in another variable.
  3. Loop from lower limit range to upper limit range using For loop.
  4. Inside the for loop take a variable say ‘num’ and initialize it’s value to  iterator value .
  5. Count how many digits there are in the number.
  6. Using mod and division operations, each digit is accessed one after the other.
  7. Each digit is multiplied by the number of digits, and the result is saved in a separate variable.
  8. Steps 6 and 7 are repeated until all of the digits have been used.
  9. Analyze the result obtained using the original number.
    • If both are same/equal then it is armstrong number
    • Else it is not armstrong number.
  10. Print the number if it is an Armstrong number in a Given range.
  11. The Exit of the Program.

Below is the implementation:

# Give the lower limit range as static input and store it in a variable.
gvn_lower_lmt = 50
# Give the upper limit range as static input and store it in another variable.
gvn_upper_lmt = 1800
# Loop from lower limit range to upper limit range using For loop.
print("The Armstrong numbers in the given range: ")
for numbr in range(gvn_lower_lmt, gvn_upper_lmt + 1):

    # Inside the for loop take a variable say 'num' and initialize it's value to
    # iterator value .
    # Count how many digits there are in the number.
    no_digits = len(str(numbr))

 # intialize result to zero(tot_sum)
    tot_sum = 0
  # copy the number in another variable(duplicate)
    tempry = numbr
    while tempry > 0:
      # getting the last digit
        rem = tempry % 10
       # multiply the result by a digit raised to the power of the number of digits
        tot_sum += rem ** no_digits
        tempry //= 10
   # It is Armstrong number if it is equal to original number
    if numbr == tot_sum:
        print(numbr)

Output:

The Armstrong numbers in the given range: 
153
370
371
407
1634

Method #2: Using For Loop (User input)

Approach:

  1. Give the lower limit range as user input and store it in a variable.
  2. Give the upper limit range as user input and store it in another variable.
  3. Loop from lower limit range to upper limit range using For loop.
  4. Inside the for loop take a variable say ‘num’ and initialize it’s value to  iterator value .
  5. Count how many digits there are in the number.
  6. Using mod and division operations, each digit is accessed one after the other.
  7. Each digit is multiplied by the number of digits, and the result is saved in a separate variable.
  8. Steps 6 and 7 are repeated until all of the digits have been used.
  9. Analyze the result obtained using the original number.
    • If both are same/equal then it is armstrong number
    • Else it is not armstrong number.
  10. Print the number if it is an Armstrong number in a Given range.
  11. The Exit of the Program.

Below is the implementation:

# Give the lower limit range as user input and store it in a variable.
gvn_lower_lmt = int(input("Enter some Random number = "))
# Give the upper limit range as user input and store it in another variable.
gvn_upper_lmt = int(input("Enter some Random number = "))
# Loop from lower limit range to upper limit range using For loop.
print("The Armstrong numbers in the given range: ")
for numbr in range(gvn_lower_lmt, gvn_upper_lmt + 1):

    # Inside the for loop take a variable say 'num' and initialize it's value to
    # iterator value .
    # Count how many digits there are in the number.
    no_digits = len(str(numbr))

 # intialize result to zero(tot_sum)
    tot_sum = 0
  # copy the number in another variable(duplicate)
    tempry = numbr
    while tempry > 0:
      # getting the last digit
        rem = tempry % 10
       # multiply the result by a digit raised to the power of the number of digits
        tot_sum += rem ** no_digits
        tempry //= 10
   # It is Armstrong number if it is equal to original number
    if numbr == tot_sum:
        print(numbr)

Output:

Enter some Random number = 500
Enter some Random number = 2000
The Armstrong numbers in the given range: 
1634

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.