Check Palindrome Number
Check an integer palindrome with a digit-reversal loop. Students preserve the original magnitude, build its reverse, and compare numeric values after the loop.
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
In this one
- Preserve an original value
- Build a numeric reverse
- Define sign-ignored symmetry
How it works
- 1
Save abs(num) in original and copy temp.
- 2
Reverse temp's digits into reversed_num.
- 3
Compare original with the completed reverse.
Edge cases
Is -121 accepted?
Is -121 accepted?
Yes. This version compares absolute values.
Is zero accepted?
Is zero accepted?
Yes. Both original and the untouched reverse equal zero.
What's going on
A numeric palindrome has the same decimal digits in both directions. This implementation checks the absolute value, so the sign is not part of the comparison.
original remains unchanged while temp is consumed. The reverse is built by shifting and appending each last digit. For 121, both values end at 121. For 120, the reverse is 21, so the comparison fails.
Is -121 accepted? Yes. This version compares absolute values.
Is zero accepted? Yes. Both original and the untouched reverse equal zero.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why keep original separately?
Pick an answer to check it.
Question 2
Is -121 accepted?
Pick an answer to check it.
Questions people ask
Is -121 accepted?
Yes. This version compares absolute values.
Is zero accepted?
Yes. Both original and the untouched reverse equal zero.
Why keep original separately?
temp is shortened during reversal.
๐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