Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check Middle Digit

Compare the middle digit with both outside digits of a three-digit number. This Python exercise teaches strict comparisons and how ties affect a classification.

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: 215
Middle digit is smallest

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

In this one

  • Identify the tens position
  • Compare strict extremes
  • Recognize ties

How it works

  1. 1

    Extract the three decimal digits.

  2. 2

    Test whether the middle beats both outer digits.

  3. 3

    Otherwise test smallest, then print the neither case.

Edge cases

What happens for 152?

What happens for 152?

The middle digit 5 is largest.

What happens for 151?

What happens for 151?

The middle digit is also largest; equal outside digits do not matter.

What's going on

The tens digit is the middle position, not the middle value after sorting. Integer division and remainder extract the three positions without converting the number to text.

The first branch requires the tens digit to exceed both others. The second requires it to be below both. A tie reaches the final message because both comparisons are strict; equality does not qualify as strictly largest or smallest.

What happens for 152? The middle digit 5 is largest.

What happens for 151? The middle digit is also largest; equal outside digits do not matter.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which number has the smallest middle digit?

Pick an answer to check it.

Question 2

What happens for 152?

Pick an answer to check it.

Questions people ask

What happens for 152?

The middle digit 5 is largest.

What happens for 151?

The middle digit is also largest; equal outside digits do not matter.

Which number has the smallest middle digit?

215.