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

Check if All Digits are Distinct

Extract the hundreds, tens, and units of a three-digit integer. Students use Python division and remainder to check every pair of digits for a repeated value.

IntermediatePhase 1 - Level 3: Math and Number LogicExample 1 of 10
check-if-all-digits-are-distinct.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 if all distinct
10if digit1 != digit2 and digit2 != digit3 and digit1 != digit3:
11 print("All digits are distinct")
12else:
13 print("Digits are not all distinct")

Output

Enter a 3-digit number: 123
All digits are distinct

Enter a 3-digit number: 122
Digits are not all distinct

In this one

  • Extract decimal places
  • Compare all digit pairs
  • Understand fixed-width assumptions

How it works

  1. 1

    Read a three-digit integer.

  2. 2

    Extract hundreds, tens, and units.

  3. 3

    Require all three pairwise inequalities for distinct digits.

Edge cases

Does 121 pass?

Does 121 pass?

No. The first and last digits are equal.

Can I enter 12?

Can I enter 12?

The code runs but treats the hundreds place as zero, outside the stated three-digit exercise.

What's going on

Distinct digits means no two digit positions contain the same value. num // 100 extracts hundreds, (num // 10) % 10 extracts tens, and num % 10 extracts units.

All three unequal comparisons must succeed. Checking only adjacent digits would miss 121, whose first and last digits repeat. The arithmetic assumes a positive three-digit input; the prompt is not a validation rule.

Does 121 pass? No. The first and last digits are equal.

Can I enter 12? The code runs but treats the hundreds place as zero, outside the stated three-digit exercise.

Quiz

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

Question 1

Why compare the first digit with the last?

Pick an answer to check it.

Question 2

Does 121 pass?

Pick an answer to check it.

Questions people ask

Does 121 pass?

No. The first and last digits are equal.

Can I enter 12?

The code runs but treats the hundreds place as zero, outside the stated three-digit exercise.

Why compare the first digit with the last?

Repeated digits need not be adjacent.