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.
1# Take 3-digit number2num = int(input("Enter a 3-digit number: "))34# Extract digits5digit1 = num // 1006digit2 = (num // 10) % 107digit3 = num % 1089# Check middle digit10if 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
Extract the three decimal digits.
- 2
Test whether the middle beats both outer digits.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum