 # Python Program to Find the 2nd Largest Digit in a Given Number

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:

The given number's { 1732981 } second largest digit = 8

Example2:

Input:

Given Number = 76542316

Output:

The given number's { 76542316 } second largest digit = 6

## Program to Find the 2nd Largest Digit in a Given Number in Python

Below are the ways to find the given number’s second-largest digit:

### Method #1: Using sort() Function (Static Input)

Approach:

• Give the number as static input and store it in a variable.
• Convert the given number to a string using the str() function and store it in another variable.
• Create a list of digits say “digtslst” using map(),list(),int functions.
• Store it in another variable.
• Sort the above-obtained list of digits using the sort() function.
• Calculate the digit at the index length of the digitlist-2 to get the second-largest digit of a given number.
• Store it in another variable.
• Print the second-largest digit of a given number.
• The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numb = 1732981
# Convert the given number to string using the str() function and store it in another variable
stringnum = str(numb)
# Create a list of digits say "digtslst" using map(),list(),int functions.
# Store it in another variable.
digtslst = list(map(int, stringnum))
# Sort the above-obtained list of digits using the sort() function.
digtslst.sort()
# Calculate the length of the list of digits of a given number using the len() function
# and store it in another variable.
len_digtslst = len(digtslst)
# Calculate the digit at the index length of the digitlist-2 to get the second-largest
# digit of a given number.
# Store it in another variable.
scnd_largst_digtt = digtslst[len_digtslst-2]
# Print the second-largest digit of a given Number.
print("The given number's {", numb,
"} second largest digit =", scnd_largst_digtt)


Output:

The given number's { 1732981 } second largest digit = 8

The given number’s { 1732981 } second largest digit = 8

### Method #2: Using sort() Function (User Input)

Approach:

• Give the number as user input using the int(input()) function and store it in a variable.
• Convert the given number to a string using the str() function and store it in another variable.
• Create a list of digits say “digtslst” using map(),list(),int functions.
• Store it in another variable.
• Sort the above-obtained list of digits using the sort() function.
• Calculate the digit at the index length of the digitlist-2 to get the second-largest digit of a given number.
• Store it in another variable.
• Print the second-largest digit of a given number.
• The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
numb = int(input("Enter some random variable = "))
# Convert the given number to string using the str() function and store it in another variable
stringnum = str(numb)
# Create a list of digits say "digtslst" using map(),list(),int functions.
# Store it in another variable.
digtslst = list(map(int, stringnum))
# Sort the above-obtained list of digits using the sort() function.
digtslst.sort()
# Calculate the length of the list of digits of a given number using the len() function
# and store it in another variable.
len_digtslst = len(digtslst)
# Calculate the digit at the index length of the digitlist-2 to get the second-largest
# digit of a given number.
# Store it in another variable.
scnd_largst_digtt = digtslst[len_digtslst-2]
# Print the second-largest digit of a given Number.
print("The given number's {", numb,
"} second largest digit =", scnd_largst_digtt)


Output:

Enter some random variable = 76542316
The given number's { 76542316 } second largest digit = 6

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.