Dremendo Tag Line

Input number and print largest and smallest digit in it using for loop in Python

for Loop - Question 13

In this question, we will see how to input a number and print the largest and the smallest digit in Python programming using for loop. To know more about for loop click on the for loop lesson.

Q13) Write a program in Python to input a number and print the largest and the smallest digit present in it using for loop.

Program

import itertools

ld=-1
sd=10
n=int(input('Enter a number '))

for i in itertools.repeat(1):
    if n>0:
        d=n%10
        if d>ld:
            ld=d        # storing the largest digit

        if d<sd:
            sd=d        # storing the smallest digit

        n=n//10         # removing the last digit from the number using floor division

    else:
        break;

print('Largest Digit = %d' %(ld))
print('Smallest Digit = %d' %(sd))

Output

Enter a number 521964
Largest Digit = 9
Smallest Digit = 1
video-poster