0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Reverse a Number with Output Example

Use $reversed = -join ($number.ToString().ToCharArray() | Reverse) to reverse a number in PowerShell by converting it to a string, reversing the characters, and joining them back.
📋

Examples

Input12345
Output54321
Input1000
Output0001
Input7
Output7
🧠

How to Think About It

To reverse a number, first treat it like text by converting it to a string. Then flip the order of each character from end to start. Finally, join these characters back into a string to get the reversed number.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string.
3
Split the string into individual characters.
4
Reverse the order of these characters.
5
Join the reversed characters back into a string.
6
Output the reversed string.
💻

Code

powershell
$number = 12345
$reversed = -join ($number.ToString().ToCharArray() | Reverse-Object)
Write-Output $reversed
Output
54321
🔍

Dry Run

Let's trace reversing the number 12345 through the code

1

Convert number to string

12345 becomes '12345'

2

Split string into characters

'12345' becomes ['1','2','3','4','5']

3

Reverse characters and join

['1','2','3','4','5'] reversed is ['5','4','3','2','1'], joined to '54321'

StepCharacters
Original1 2 3 4 5
Reversed5 4 3 2 1
💡

Why This Works

Step 1: Convert number to string

Using ToString() changes the number into text so we can handle each digit separately.

Step 2: Reverse characters

The Reverse-Object command flips the order of the characters in the array.

Step 3: Join reversed characters

Using -join combines the reversed characters back into a single string representing the reversed number.

🔄

Alternative Approaches

Using arithmetic operations
powershell
$number = 12345
$reversed = 0
while ($number -gt 0) {
    $digit = $number % 10
    $reversed = $reversed * 10 + $digit
    $number = [math]::Floor($number / 10)
}
Write-Output $reversed
This method reverses the number mathematically without converting to string, but is more complex.
Using array indexing
powershell
$number = 12345
$string = $number.ToString()
$reversed = ''
for ($i = $string.Length - 1; $i -ge 0; $i--) {
    $reversed += $string[$i]
}
Write-Output $reversed
This manually builds the reversed string by looping backward through characters.

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

Time Complexity

Reversing the string requires visiting each digit once, so time grows linearly with the number of digits.

Space Complexity

Extra space is needed to hold the characters array and the reversed string, proportional to the number of digits.

Which Approach is Fastest?

The string reversal method is simple and fast for typical use, while arithmetic reversal avoids string overhead but is more complex.

ApproachTimeSpaceBest For
String reversal with ReverseO(n)O(n)Simplicity and readability
Arithmetic reversalO(n)O(1)Pure numeric operations without string conversion
Manual loop with indexingO(n)O(n)Control over reversal process
💡
Convert the number to a string first to easily reverse its digits using built-in array commands.
⚠️
Trying to reverse the number directly as an integer without converting to string causes errors or unexpected results.