Python Program to Find the Greatest Digit in a Number.

In the previous article, we have discussed Python Program for Comma-separated String to Tuple
Given a number, and the task is to find the greatest digit in a given number.

Example: Let the number is 149.

The greatest digit in a given number is ‘9’

Examples:

Example1:

Input:

Given number = 639

Output:

The maximum digit in given number { 639 } =  9

Example2:

Input:

Given number = 247

Output:

The maximum digit in given number { 247 } =  7

Program to Find the Greatest Digit in a Number.

Below are the ways to find the greatest digit in a given number.

Method #1: Using list() Function (Static input)

Approach:

  • Give the number as static input and store it in a variable.
  • Convert the given number into the string using the str() function and store it in another variable.
  • Convert the above-obtained string number into a list of digits using the built-in list() method and store it in another variable.
  • Find the maximum list of digits using the built-in max() function and store it in another variable.
  • Print the greatest digit in a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_num = 154
# Convert the given number into string using str() function and
# store it in another variable. 
str_numbr = str(gvn_num)
# Convert the above obtained string number into list of digits using bulit-in list()
# method and store it in another variable.
lst = list(str_numbr)
# Find the maximum of list of digits using bulit-in max() function
# and store it in another variable.
maxim_digit = max(lst)
# Print the greatest digit in a given number.
print("The maximum digit in given number {", gvn_num, "} = ", maxim_digit)

Output:

The maximum digit in given number { 154 } =  5

Method #2: Using list() 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 into the string using the str() function and store it in another variable.
  • Convert the above-obtained string number into a list of digits using the built-in list() method and store it in another variable.
  • Find the maximum list of digits using the built-in max() function and store it in another variable.
  • Print the greatest digit in a given number.
  • The Exit of the program.

Below is the implementation:

#Give the number as user input using int(input()) and store it in a variable.
gvn_num = int(input("Enter some random number = "))
# Convert the given number into string using str() function and
# store it in another variable. 
str_numbr = str(gvn_num)
# Convert the above obtained string number into list of digits using bulit-in list()
# method and store it in another variable.
lst = list(str_numbr)
# Find the maximum of list of digits using bulit-in max() function
# and store it in another variable.
maxim_digit = max(lst)
# Print the greatest digit in a given number.
print("The maximum digit in given number {", gvn_num, "} = ", maxim_digit)

Output:

Enter some random number = 183
The maximum digit in given number { 183 } = 8

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.