Check Palindrome Number
Check if a number is palindrome.
IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 3 of 14
check-palindrome-number.py
1# Take number2num = int(input("Enter a number: "))34# Reverse number5original = abs(num)6reversed_num = 07temp = original89while temp > 0:10 last_digit = temp % 1011 reversed_num = reversed_num * 10 + last_digit12 temp //= 101314# Check palindrome15if original == reversed_num:16 print("Palindrome")17else:18 print("Not a palindrome")
Output
Enter a number: 121 Palindrome Enter a number: 123 Not a palindrome
What's going on
Reverse number and compare with original.
Key Concepts:
Reverse the number
Compare reversed with original
If equal, it's a palindrome
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum