0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Check Armstrong Number

Use a PowerShell script that calculates the sum of each digit raised to the power of the number of digits and compares it to the original number, like $sum = 0; $digits = $num.ToString().ToCharArray(); foreach ($d in $digits) { $sum += [math]::Pow([int]$d, $digits.Length) }; if ($sum -eq $num) { 'Armstrong' } else { 'Not Armstrong' }.
📋

Examples

Input153
Output153 is an Armstrong number.
Input9474
Output9474 is an Armstrong number.
Input123
Output123 is not an Armstrong number.
🧠

How to Think About It

To check if a number is Armstrong, split it into digits, count how many digits it has, then raise each digit to that power and add them all. If the sum equals the original number, it is Armstrong.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string and split into digits.
3
Count the number of digits.
4
For each digit, raise it to the power of the digit count and add to a sum.
5
Compare the sum to the original number.
6
Return if the number is Armstrong or not.
💻

Code

powershell
$num = Read-Host 'Enter a number'
$digits = $num.ToString().ToCharArray()
$power = $digits.Length
$sum = 0
foreach ($d in $digits) {
    $sum += [math]::Pow([int]$d, $power)
}
if ($sum -eq [int]$num) {
    Write-Output "$num is an Armstrong number."
} else {
    Write-Output "$num is not an Armstrong number."
}
Output
Enter a number: 153 153 is an Armstrong number.
🔍

Dry Run

Let's trace the number 153 through the code

1

Split digits

Digits = ['1', '5', '3'], Power = 3

2

Calculate sum

Sum = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

3

Compare sum

Sum 153 equals original number 153, so it is Armstrong

DigitDigit^PowerRunning Sum
111
5125126
327153
💡

Why This Works

Step 1: Split number into digits

We convert the number to a string and split it so we can work with each digit separately using ToCharArray().

Step 2: Calculate power sum

Each digit is raised to the power of the total number of digits using [math]::Pow() and added to a sum.

Step 3: Compare sum to original

If the sum equals the original number, it confirms the number is Armstrong.

🔄

Alternative Approaches

Using array pipeline and Measure-Object
powershell
$num = Read-Host 'Enter a number'
$digits = $num.ToString().ToCharArray() | ForEach-Object { [int]$_ }
$power = $digits.Length
$sum = ($digits | ForEach-Object { [math]::Pow($_, $power) }) | Measure-Object -Sum | Select-Object -ExpandProperty Sum
if ($sum -eq [int]$num) { Write-Output "$num is an Armstrong number." } else { Write-Output "$num is not an Armstrong number." }
Uses PowerShell pipeline and Measure-Object for summing, which is more idiomatic but slightly longer.
Using a function for reuse
powershell
function Is-Armstrong($num) {
  $digits = $num.ToString().ToCharArray()
  $power = $digits.Length
  $sum = 0
  foreach ($d in $digits) { $sum += [math]::Pow([int]$d, $power) }
  return $sum -eq $num
}
$num = Read-Host 'Enter a number'
if (Is-Armstrong([int]$num)) { Write-Output "$num is an Armstrong number." } else { Write-Output "$num is not an Armstrong number." }
Encapsulates logic in a function for better code reuse and clarity.

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

Time Complexity

The script loops through each digit once, so time grows linearly with the number of digits, O(n).

Space Complexity

It stores digits in an array, so space also grows linearly with digit count, O(n).

Which Approach is Fastest?

All approaches have similar time and space complexity; using pipelines may be slightly slower but more readable.

ApproachTimeSpaceBest For
Basic loopO(n)O(n)Simplicity and clarity
Pipeline with Measure-ObjectO(n)O(n)PowerShell idiomatic style
Function encapsulationO(n)O(n)Code reuse and modularity
💡
Always convert digits to integers before raising to power to avoid errors.
⚠️
Forgetting to convert digit characters to integers before using math operations causes wrong results.