{"id":19001,"date":"2021-08-29T15:13:12","date_gmt":"2021-08-29T09:43:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=19001"},"modified":"2021-11-22T18:37:09","modified_gmt":"2021-11-22T13:07:09","slug":"python-program-to-find-the-previous-armstrong-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-previous-armstrong-number\/","title":{"rendered":"Python Program to Find the Previous Armstrong Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Next Armstrong Number<\/a> 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\u2019s digits.<\/p>\n Itertools count() function:<\/strong><\/p>\n 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.<\/p>\n In the absence of a breaking condition, the count() method will continue counting indefinitely (on a system with infinite memory)<\/p>\n Given a number, the task is to print the previous Armstrong number in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the previous Armstrong number in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nArmstrong Number:<\/strong><\/p>\nGiven Number =67<\/pre>\n
The previous armstrong number of { 67 } is : 9<\/pre>\n
Given Number =450<\/pre>\n
The previous armstrong number of { 450 } is : 407<\/pre>\n
Program to Find the Previous Armstrong Number in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Import the count from the itertools module using the import and from keyword.\r\nfrom itertools import count\r\n# Give the number n as static input and store it in a variable.\r\ngvnnumb = 67\r\n# Loop till the given number in decreasing order using the For loop\r\n# and the count() function.\r\nfor m in count(gvnnumb, -1):\r\n # Convert the given number to the string using the str() function\r\n # and store it in a variable say strngnumb.\r\n strngnumb = str(m)\r\n # Take a variable say cuntsum and initialize its value to 0.\r\n cuntsum = 0\r\n # Calculate the length of the string using the len() function\r\n # and store it in a variable to say\r\n strnglengt = len(strngnumb)\r\n # Loop in the strngnumb using the nested For loop(Inner For loop).\r\n for dgtval in strngnumb:\r\n # Convert the inner loop iterator value to an integer using the int() function\r\n # and store it as a new variable say intitr.\r\n intitr = int(dgtval)\r\n # Inside the for loop, increment the value of cuntsum with intitr**strnglengt(where ** is power operator).\r\n cuntsum += intitr**strnglengt\r\n # After the end of For loop check if cuntsum is equal to the iterator value\r\n # of the inner for loop using the if conditional statement.\r\n\r\n if(cuntsum == m):\r\n # If it is true then print the value of cuntsum.\r\n print('The previous armstrong number of {', gvnnumb, '} is :', cuntsum)\r\n # Break the parent for loop using the break keyword.\r\n break\r\n<\/pre>\n
The previous armstrong number of { 67 } is : 9<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Import the count from the itertools module using the import and from keyword.\r\nfrom itertools import count\r\n# Give the number n as user input using the int(input()) function and store it in a variable.\r\ngvnnumb = int(input('Enter some random number = '))\r\n# Loop till the given number in decreasing order using the For loop\r\n# and the count() function.\r\nfor m in count(gvnnumb, -1):\r\n # Convert the given number to the string using the str() function\r\n # and store it in a variable say strngnumb.\r\n strngnumb = str(m)\r\n # Take a variable say cuntsum and initialize its value to 0.\r\n cuntsum = 0\r\n # Calculate the length of the string using the len() function\r\n # and store it in a variable to say\r\n strnglengt = len(strngnumb)\r\n # Loop in the strngnumb using the nested For loop(Inner For loop).\r\n for dgtval in strngnumb:\r\n # Convert the inner loop iterator value to an integer using the int() function\r\n # and store it as a new variable say intitr.\r\n intitr = int(dgtval)\r\n # Inside the for loop, increment the value of cuntsum with intitr**strnglengt(where ** is power operator).\r\n cuntsum += intitr**strnglengt\r\n # After the end of For loop check if cuntsum is equal to the iterator value\r\n # of the inner for loop using the if conditional statement.\r\n\r\n if(cuntsum == m):\r\n # If it is true then print the value of cuntsum.\r\n print('The previous armstrong number of {', gvnnumb, '} is :', cuntsum)\r\n # Break the parent for loop using the break keyword.\r\n break\r\n<\/pre>\n
Enter some random number = 450\r\nThe previous armstrong number of { 450 } is : 407<\/pre>\n
\n