Check First and Last Digits Equal
Take a 4-digit number and check if the first and last digits are equal.
IntermediatePhase 1 - Level 3: Math and Number LogicExample 3 of 10
check-first-and-last-digits-equal.py
1# Take 4-digit number2num = int(input("Enter a 4-digit number: "))34# Extract first and last digits5first_digit = num // 10006last_digit = num % 1078# Check if equal9if first_digit == last_digit:10 print("First and last digits are equal")11else:12 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
What's going on
Extract first (thousands) and last (units) digits.
Key Concepts:
First digit: num // 1000
Last digit: num % 10
Compare for equality
🔗Related Content
- 📖
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- 💻
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum