Logic Building
# Take 3-digit number
num = int(input("Enter a 3-digit number: "))
# Extract digits
digit1 = num // 100
digit2 = (num // 10) % 10
digit3 = num % 10
# Check middle digit
if digit2 > digit1 and digit2 > digit3:
print("Middle digit is largest")
elif digit2 < digit1 and digit2 < digit3:
print("Middle digit is smallest")
else:
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
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