Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 3 of 14
check-palindrome-number.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Reverse number
5original = abs(num)
6reversed_num = 0
7temp = original
8
9while temp > 0:
10 last_digit = temp % 10
11 reversed_num = reversed_num * 10 + last_digit
12 temp //= 10
13
14# Check palindrome
15if 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. 1

    Save abs(num) in original and copy temp.

  2. 2

    Reverse temp's digits into reversed_num.

  3. 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.