Python Program to find largest and smallest digit of a number

In this program, You will learn how to find the largest and smallest digit of a number in Python.


while <condition>:
      #statement

Python Program to find largest and smallest digit of a number

Example: How to find the largest and smallest digit of a number in Python.

n = int(input("Enter a number:"))

sd = 9
ld = 0

while n > 0:
    r = n % 10
    if sd > r:
        sd = r
    if ld < r:
        ld = r
    n = int(n / 10)

print("Largest digit:", ld)
print("Smallest digit:", sd)

Output:

Enter a number:3254
Largest digit: 5
Smallest digit: 2