{"id":18507,"date":"2021-08-26T10:39:04","date_gmt":"2021-08-26T05:09:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=18507"},"modified":"2021-11-22T18:37:17","modified_gmt":"2021-11-22T13:07:17","slug":"python-program-to-find-the-2nd-largest-digit-in-a-given-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-2nd-largest-digit-in-a-given-number\/","title":{"rendered":"Python Program to Find the 2nd Largest Digit in a Given Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find GCD of Elements in a Given Range<\/a> sort() method in Python:<\/strong><\/p>\n By default, the sort() method sorts the list in ascending order.<\/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 given number’s second-largest digit:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The given number’s { 1732981 } second largest digit = 8<\/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 GCD of Elements in a Given Range Given a number and the task is to find the given number’s second-largest digit. sort() method in Python: By default, the sort() method sorts the list in ascending order. Examples: Example1: Input: Given Number = 1732981 Output: …<\/p>\n
\nGiven a number and the task is to find the given number’s second-largest digit.<\/p>\nGiven Number = 1732981<\/pre>\n
The given number's { 1732981 } second largest digit = 8<\/pre>\n
Given Number = 76542316<\/pre>\n
The given number's { 76542316 } second largest digit = 6<\/pre>\n
Program to Find the 2nd Largest Digit in a Given Number in Python<\/h2>\n
\n
Method #1: Using sort() Function (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\nnumb = 1732981\r\n# Convert the given number to string using the str() function and store it in another variable\r\nstringnum = str(numb)\r\n# Create a list of digits say \"digtslst\" using map(),list(),int functions.\r\n# Store it in another variable.\r\ndigtslst = list(map(int, stringnum))\r\n# Sort the above-obtained list of digits using the sort() function.\r\ndigtslst.sort()\r\n# Calculate the length of the list of digits of a given number using the len() function\r\n# and store it in another variable.\r\nlen_digtslst = len(digtslst)\r\n# Calculate the digit at the index length of the digitlist-2 to get the second-largest\r\n# digit of a given number.\r\n# Store it in another variable.\r\nscnd_largst_digtt = digtslst[len_digtslst-2]\r\n# Print the second-largest digit of a given Number.\r\nprint(\"The given number's {\", numb,\r\n \"} second largest digit =\", scnd_largst_digtt)\r\n<\/pre>\n
The given number's { 1732981 } second largest digit = 8<\/pre>\n
Method #2: Using sort() Function (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\nnumb = int(input(\"Enter some random variable = \"))\r\n# Convert the given number to string using the str() function and store it in another variable\r\nstringnum = str(numb)\r\n# Create a list of digits say \"digtslst\" using map(),list(),int functions.\r\n# Store it in another variable.\r\ndigtslst = list(map(int, stringnum))\r\n# Sort the above-obtained list of digits using the sort() function.\r\ndigtslst.sort()\r\n# Calculate the length of the list of digits of a given number using the len() function\r\n# and store it in another variable.\r\nlen_digtslst = len(digtslst)\r\n# Calculate the digit at the index length of the digitlist-2 to get the second-largest\r\n# digit of a given number.\r\n# Store it in another variable.\r\nscnd_largst_digtt = digtslst[len_digtslst-2]\r\n# Print the second-largest digit of a given Number.\r\nprint(\"The given number's {\", numb,\r\n \"} second largest digit =\", scnd_largst_digtt)\r\n<\/pre>\n
Enter some random variable = 76542316\r\nThe given number's { 76542316 } second largest digit = 6<\/pre>\n
\n