Check Palindrome Recursively
Check if a number is palindrome recursively.
IntermediatePhase 3 - Level 2: Advanced RecursionExample 3 of 10
check-palindrome-recursively.py
1def reverse_number(n, rev=0):2 if n == 0:3 return rev4 last_digit = n % 105 rev = rev * 10 + last_digit6 return reverse_number(n // 10, rev)78# Check palindrome9num = int(input("Enter a number: "))10reversed_num = reverse_number(abs(num))11if abs(num) == reversed_num:12 print("Palindrome")13else:14 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:
Use reverse function recursively
Compare reversed with original
If equal, palindrome
🔗Related Content
- 📖
Phase 3 - Learn Concepts
Review Phase 3 concepts and explanations
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with quiz questions
- 💻
Phase 3 - All Practice Problems
Explore all practice problems for Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum