0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Check Palindrome Number

Use a PowerShell script that converts the number to a string, reverses it with -join and Reverse, then compares it to the original string to check if it's a palindrome.
📋

Examples

Input121
Output121 is a palindrome number.
Input123
Output123 is not a palindrome number.
Input1
Output1 is a palindrome number.
🧠

How to Think About It

To check if a number is a palindrome, think of it as a word. Convert the number to text, then reverse that text. If the reversed text matches the original, the number reads the same forwards and backwards, so it is a palindrome.
📐

Algorithm

1
Get the input number as a string.
2
Reverse the string characters.
3
Compare the reversed string with the original string.
4
If they are the same, the number is a palindrome; otherwise, it is not.
💻

Code

powershell
function Test-PalindromeNumber {
    param([int]$number)
    $str = $number.ToString()
    $rev = -join ($str.ToCharArray() | Reverse)
    if ($str -eq $rev) {
        Write-Output "$number is a palindrome number."
    } else {
        Write-Output "$number is not a palindrome number."
    }
}

Test-PalindromeNumber -number 121
Test-PalindromeNumber -number 123
Output
121 is a palindrome number. 123 is not a palindrome number.
🔍

Dry Run

Let's trace the number 121 through the code

1

Convert number to string

Input: 121 -> String: '121'

2

Reverse string characters

'121' reversed -> '121'

3

Compare original and reversed

'121' equals '121' -> True, so palindrome

Original StringReversed StringPalindrome?
121121Yes
💡

Why This Works

Step 1: Convert number to string

Using ToString() changes the number into text so we can check each character.

Step 2: Reverse the string

We split the string into characters, reverse their order with Reverse, then join them back with -join.

Step 3: Compare original and reversed

If the reversed string matches the original, the number reads the same forwards and backwards, so it is a palindrome.

🔄

Alternative Approaches

Using arithmetic to reverse number
powershell
function Test-PalindromeNumberArithmetic {
    param([int]$number)
    $original = $number
    $reverse = 0
    while ($number -gt 0) {
        $digit = $number % 10
        $reverse = $reverse * 10 + $digit
        $number = [math]::Floor($number / 10)
    }
    if ($original -eq $reverse) {
        Write-Output "$original is a palindrome number."
    } else {
        Write-Output "$original is not a palindrome number."
    }
}

Test-PalindromeNumberArithmetic -number 121
Test-PalindromeNumberArithmetic -number 123
This method uses math operations without converting to string, which can be faster for large numbers but is more complex to write.
Using string comparison with -eq operator
powershell
function Test-PalindromeNumberSimple {
    param([int]$number)
    $str = $number.ToString()
    if ($str -eq (-join ($str.ToCharArray() | Reverse))) {
        Write-Output "$number is a palindrome number."
    } else {
        Write-Output "$number is not a palindrome number."
    }
}

Test-PalindromeNumberSimple -number 121
Test-PalindromeNumberSimple -number 123
This is a concise version of the string reversal method, good for readability.

Complexity: O(n) time, O(n) space

Time Complexity

The script processes each digit once to reverse the string, so time grows linearly with the number of digits.

Space Complexity

Extra space is used to store the reversed string, proportional to the number of digits.

Which Approach is Fastest?

The arithmetic method avoids string operations and can be faster for very large numbers, but the string method is simpler and clearer for most cases.

ApproachTimeSpaceBest For
String reversalO(n)O(n)Simplicity and readability
Arithmetic reversalO(n)O(1)Performance with large numbers
💡
Always convert the number to a string first to easily reverse and compare characters.
⚠️
Forgetting to convert the number to a string before reversing causes errors or incorrect results.