Python: Largest and smallest digit of a given number
Python Basic - 1: Exercise-145 with Solution
Write a Python program to find the largest and smallest digits of a given number.
Sample Solution:
Python Code:
def Largest_Smallest_digit(n):
largest_digit = 0
smallest_digit = 9
while (n):
digit = n % 10
# largest digit
largest_digit = max(digit, largest_digit)
# smallest digit
smallest_digit = min(digit, smallest_digit)
n = n // 10
return largest_digit, smallest_digit
n = 9387422
print("Original Number:", n)
result = Largest_Smallest_digit(n)
print("Largest Digit of the said number:", result[0])
print("Smallest Digit of the said number:", result[1])
n = 500
print("\nOriginal Number:", n)
result = Largest_Smallest_digit(n)
print("Largest Digit of the said number:", result[0])
print("Smallest Digit of the said number:", result[1])
n = 231548
print("\nOriginal Number:", n)
result = Largest_Smallest_digit(n)
print("Largest Digit of the said number:", result[0])
print("Smallest Digit of the said number:", result[1])
Sample Output:
Original Number: 9387422 Largest Digit of the said number: 9 Smallest Digit of the said number: 2 Original Number: 500 Largest Digit of the said number: 5 Smallest Digit of the said number: 0 Original Number: 231548 Largest Digit of the said number: 8 Smallest Digit of the said number: 1
Flowchart:

Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to convert integer to string.
Next: Check square root and cube root of a number
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join