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
- Take the input number as a string.
- Reverse the entire string, character by character.
- Compare the reversed string to the original.
- 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
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 = 1 → 12 → 121. Because each character is placed at the front, the final string is the mirror image of the original.
Why Check for Palindromes?
- ID Verification: Some systems use symmetrical codes for visual verification (e.g., ticket numbers like
50205). - Mathematical Analysis: Palindromic numbers have interesting properties in number theory, such as their connection to prime numbers.
- String Logic Training: This exercise strengthens your understanding of string reversal and character-by-character processing in Batch.
Important Considerations
- Leading Zeros: A number like
010has its leading zero stripped byset /a, becoming10. Its reverse would be01(or1), making it fail the palindrome check. If you are working with zero-padded numbers, keep them as strings and avoidset /a. - Negative Numbers:
-121reversed is121-, which is not identical. Palindrome checks are typically performed on positive integers only. - 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.
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
- Input Validation: Before checking, verify that the input is a valid numeric string. Non-numeric characters will produce meaningless results.
- 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.
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.