Skip to content

S1Q1 · Tens Digit Greater Than Ones Digit

⚡ Quick Reference

Function: is_tens_digit_greater(num: int) -> bool

Core idea: Extract digits using // and %, then compare.

def is_tens_digit_greater(num):
    return (num // 10) > (num % 10)

Key rules: - Tens digit = num // 10 - Ones digit = num % 10 - Return True only if tens digit is strictly greater


Problem Statement

Problem

Write a function is_tens_digit_greater(num) that takes a positive two-digit integer and returns True if the tens digit is strictly greater than the ones digit, False otherwise.

Examples:

Call
is_tens_digit_greater(54)
Output
True
Call
is_tens_digit_greater(33)
Output
False
Call
is_tens_digit_greater(27)
Output
False

Understanding the problem

For any two-digit number, integer division and modulo extract the two digits:

num num // 10 (tens) num % 10 (ones) tens > ones?
54 5 4 ✅ True
33 3 3 ✗ False (equal, not greater)
27 2 7 ✗ False
91 9 1 ✅ True

// and % are the standard digit extraction tools

For a two-digit number: num // 10 drops the ones place (integer division by 10), and num % 10 isolates the ones place (remainder when divided by 10). This generalises -- for any n-digit number, % 10 always gives the last digit and // 10 removes it.


Solution approaches

def is_tens_digit_greater(num: int) -> bool:
    tens = num // 10    # integer division removes the ones digit
    ones = num % 10     # remainder gives the ones digit
    return tens > ones
def is_tens_digit_greater(num: int) -> bool:
    return (num // 10) > (num % 10)
is_tens_digit_greater = lambda num: (num // 10) > (num % 10)

Key takeaways

01

Digit extraction with // and %

num // 10 gives the tens digit; num % 10 gives the ones digit. This is the standard arithmetic approach -- no string conversion needed. It generalises to any position: // 100 % 10 gives the hundreds digit, and so on.

02

Comparison returns a bool directly

tens > ones already evaluates to True or False -- no need to wrap it in if tens > ones: return True else: return False. The comparison expression is the return value.

03

String alternative: str(num)[0]

An alternative is int(str(num)[0]) > int(str(num)[1]) -- convert to string, index the characters, convert back to int. It works but is slower and less idiomatic than the arithmetic approach for simple digit extraction.