Check First and Last Digits Equal

Take a 4-digit number and check if the first and last digits are equal.

Logic BuildingIntermediate
Logic Building
# Take 4-digit number
num = int(input("Enter a 4-digit number: "))

# Extract first and last digits
first_digit = num // 1000
last_digit = num % 10

# Check if equal
if first_digit == last_digit:
    print("First and last digits are equal")
else:
    print("First and last digits are not equal")

Output

Enter a 4-digit number: 1231
First and last digits are equal

Enter a 4-digit number: 1234
First and last digits are not equal

Extract first (thousands) and last (units) digits.

Key Concepts:

  • First digit: num // 1000
  • Last digit: num % 10
  • Compare for equality