Check if All Digits are Distinct

Take a 3-digit number and check if all digits are distinct.

Logic BuildingIntermediate
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 if all distinct
if digit1 != digit2 and digit2 != digit3 and digit1 != digit3:
    print("All digits are distinct")
else:
    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

Extract digits and compare them.

Key Concepts:

  • Extract hundreds, tens, and units digits
  • Compare all pairs: digit1 != digit2, digit2 != digit3, digit1 != digit3
  • All must be different for distinct digits