Check Middle Digit

Take a 3-digit number and determine if the middle digit is the largest, smallest, or neither.

IntermediatePhase 1 - Level 3: Math and Number LogicExample 2 of 10
check-middle-digit.py
1# Take 3-digit number
2num = int(input("Enter a 3-digit number: "))
3
4# Extract digits
5digit1 = num // 100
6digit2 = (num // 10) % 10
7digit3 = num % 10
8
9# Check middle digit
10if digit2 > digit1 and digit2 > digit3:
11 print("Middle digit is largest")
12elif digit2 < digit1 and digit2 < digit3:
13 print("Middle digit is smallest")
14else:
15 print("Middle digit is neither largest nor smallest")

Output

Enter a 3-digit number: 152
Middle digit is largest

Enter a 3-digit number: 251
Middle digit is smallest

Enter a 3-digit number: 123
Middle digit is neither largest nor smallest

What's going on

Compare middle digit with first and third digits.

Key Concepts:

Extract all three digits
Check if middle > both others (largest)
Check if middle < both others (smallest)
Otherwise, it's neither