Python Program to Find the Previous Armstrong Number

In the previous article, we have discussed Python Program to Find the Next Armstrong Number
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.

Itertools count() function:

The count(start, step) method generates an iterator that is used to generate equally spaced numbers, where the step argument defines the space between them. The start argument specifies the iterator’s starting value, which is set by default to start=0 and step=1.

In the absence of a breaking condition, the count() method will continue counting indefinitely (on a system with infinite memory)

Given a number, the task is to print the previous Armstrong number in Python.

Examples:

Example1:

Input:

Given Number =67

Output:

The previous armstrong number of { 67 } is : 9

Example2:

Input:

Given Number =450

Output:

The previous armstrong number of { 450 } is : 407

Program to Find the Previous Armstrong Number in Python

Below are the ways to find the previous Armstrong number in Python:

Method #1: Using For Loop (Static Input)

Approach:

  • Import the count from the itertools module using the import and from keyword.
  • Give the number n as static input and store it in a variable.
  • Loop till the given number in decreasing order using the For loop and the count() function.
  • Convert the given number to the string using the str() function and store it in a variable say strngnumb.
  • Take a variable say cuntsum and initialize its value to 0.
  • Loop in the strngnumb using the nested For loop(Inner For loop).
  • Calculate the length of the string using the len() function and store it in a variable to say strnglengt.
  • Convert the inner loop iterator value to an integer using the int() function and store it as a new variable say intitr.
  • Inside the for loop, increment the value of cuntsum with intitr**strnglengt(where ** is power operator).
  • After the end of For loop check if cuntsum is equal to the iterator value of the inner for loop using the if conditional statement.
  • If it is true then print the value of cuntsum.
  • Break the parent for loop using the break keyword.
  • The Exit of the program.

Below is the implementation:

# Import the count from the itertools module using the import and from keyword.
from itertools import count
# Give the number n as static input and store it in a variable.
gvnnumb = 67
# Loop till the given number in decreasing order using the For loop
# and the count() function.
for m in count(gvnnumb, -1):
        # Convert the given number to the string using the str() function
    # and store it in a variable say strngnumb.
    strngnumb = str(m)
    # Take a variable say cuntsum and initialize its value to 0.
    cuntsum = 0
    # Calculate the length of the string using the len() function
    # and store it in a variable to say
    strnglengt = len(strngnumb)
    # Loop in the strngnumb using the nested For loop(Inner For loop).
    for dgtval in strngnumb:
                # Convert the inner loop iterator value to an integer using the int() function
        # and store it as a new variable say intitr.
        intitr = int(dgtval)
        # Inside the for loop, increment the value of cuntsum with intitr**strnglengt(where ** is power operator).
        cuntsum += intitr**strnglengt
        # After the end of For loop check if cuntsum is equal to the iterator value
    # of the inner for loop using the if conditional statement.

    if(cuntsum == m):
                # If it is true then print the value of cuntsum.
        print('The previous armstrong number of {', gvnnumb, '} is :', cuntsum)
        # Break the parent for loop using the break keyword.
        break

Output:

The previous armstrong number of { 67 } is : 9

Method #2: Using For loop (User Input)

Approach:

  • Import the count from the itertools module using the import and from keyword.
  • Give the number n as user input using the int(input()) function and store it in a variable.
  • Loop till the given number in decreasing order using the For loop and the count() function.
  • Convert the given number to the string using the str() function and store it in a variable say strngnumb.
  • Take a variable say cuntsum and initialize its value to 0.
  • Loop in the strngnumb using the nested For loop(Inner For loop).
  • Calculate the length of the string using the len() function and store it in a variable to say strnglengt.
  • Convert the inner loop iterator value to an integer using the int() function and store it as a new variable say intitr.
  • Inside the for loop, increment the value of cuntsum with intitr**strnglengt(where ** is power operator).
  • After the end of For loop check if cuntsum is equal to the iterator value of the inner for loop using the if conditional statement.
  • If it is true then print the value of cuntsum.
  • Break the parent for loop using the break keyword.
  • The Exit of the program.

Below is the implementation:

# Import the count from the itertools module using the import and from keyword.
from itertools import count
# Give the number n as user input using the int(input()) function and store it in a variable.
gvnnumb = int(input('Enter some random number = '))
# Loop till the given number in decreasing order using the For loop
# and the count() function.
for m in count(gvnnumb, -1):
        # Convert the given number to the string using the str() function
    # and store it in a variable say strngnumb.
    strngnumb = str(m)
    # Take a variable say cuntsum and initialize its value to 0.
    cuntsum = 0
    # Calculate the length of the string using the len() function
    # and store it in a variable to say
    strnglengt = len(strngnumb)
    # Loop in the strngnumb using the nested For loop(Inner For loop).
    for dgtval in strngnumb:
                # Convert the inner loop iterator value to an integer using the int() function
        # and store it as a new variable say intitr.
        intitr = int(dgtval)
        # Inside the for loop, increment the value of cuntsum with intitr**strnglengt(where ** is power operator).
        cuntsum += intitr**strnglengt
        # After the end of For loop check if cuntsum is equal to the iterator value
    # of the inner for loop using the if conditional statement.

    if(cuntsum == m):
                # If it is true then print the value of cuntsum.
        print('The previous armstrong number of {', gvnnumb, '} is :', cuntsum)
        # Break the parent for loop using the break keyword.
        break

Output:

Enter some random number = 450
The previous armstrong number of { 450 } is : 407

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.