Skip to main content

How to Check if a Number is a Palindrome in Batch Script

A Palindrome Number is a number that reads the same forwards and backwards,like 121, 1331, or 12321. Checking for palindromes is a classic programming exercise that combines string reversal with comparison logic. It is also used in data validation (like verifying symmetrical ID codes) and mathematical analysis.

In this guide, we will demonstrate how to check if a number is a palindrome by reversing its string representation and comparing it to the original.

The Strategy: Reverse and Compare

  1. Take the input number as a string.
  2. Reverse the entire string, character by character.
  3. Compare the reversed string to the original.
  4. If they are identical, it is a palindrome.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set /p "num=Enter a number: "

:: 1. Reverse the number string
set "original=!num!"
set "reversed="
set "temp=!num!"

:reverse_loop
if not defined temp goto :compare

:: Prepend the first char to 'reversed'
set "reversed=!temp:~0,1!!reversed!"

:: Remove the first char
set "temp=!temp:~1!"
goto :reverse_loop

:: 2. Compare original and reversed
:compare
echo.
echo Original: !original!
echo Reversed: !reversed!

if "!original!"=="!reversed!" (
echo.
echo [RESULT] !original! IS a palindrome!
) else (
echo.
echo [RESULT] !original! is NOT a palindrome.
)

pause
tip

The reversal loop works by peeling off the first character of temp on each iteration and prepending it to reversed. For input 121, the sequence is: reversed = 112121. Because each character is placed at the front, the final string is the mirror image of the original.

Why Check for Palindromes?

  1. ID Verification: Some systems use symmetrical codes for visual verification (e.g., ticket numbers like 50205).
  2. Mathematical Analysis: Palindromic numbers have interesting properties in number theory, such as their connection to prime numbers.
  3. String Logic Training: This exercise strengthens your understanding of string reversal and character-by-character processing in Batch.

Important Considerations

  1. Leading Zeros: A number like 010 has its leading zero stripped by set /a, becoming 10. Its reverse would be 01 (or 1), making it fail the palindrome check. If you are working with zero-padded numbers, keep them as strings and avoid set /a.
  2. Negative Numbers: -121 reversed is 121-, which is not identical. Palindrome checks are typically performed on positive integers only.
  3. String vs Number: This is fundamentally a string comparison. Even though the input looks like a "Number," the logic is entirely based on character position.
warning

User input obtained via set /p is inherently untrusted. Input containing special characters like &, |, >, or ^ can break the script or execute unintended commands. When using delayed expansion, these characters are handled safely during variable reads with !var!, but the initial set /p assignment itself can still be vulnerable. Always validate input before processing it.

Best Practices

  1. Input Validation: Before checking, verify that the input is a valid numeric string. Non-numeric characters will produce meaningless results.
  2. Performance: For small numbers (under 10 digits), this script is extremely fast. For very long strings of numbers, consider using PowerShell for optimized string handling.
danger

Never use percent expansion (%var%) for string comparisons that involve user input. A value containing poison characters like & or | will be interpreted by the parser as command operators when expanded with %. Delayed expansion (!var!) treats the content as a literal string, preventing code injection and parsing failures.

Conclusion

Checking for palindromes is a clean, elegant exercise in string reversal and comparison. By treating numbers as character sequences, you unlock a class of analysis that pure arithmetic cannot provide. This technique demonstrates the dual nature of data in Batch, where every piece of information is both a number and a string, and reinforces the importance of choosing the right interpretation for each task.