How to Check if a Number is an Armstrong Number in Batch Script
An Armstrong Number (also called a Narcissistic Number) is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. For example, 153 is an Armstrong number because it has 3 digits, and $1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153$. This is a classic number theory exercise that combines digit extraction with exponentiation.
In this guide, we will demonstrate how to check if a number is an Armstrong number using digit extraction and a power-calculation subroutine.
The Strategy: Extract, Power, and Sum
- Count the number of digits ($n$).
- Extract each digit.
- Raise each digit to the power of $n$.
- Sum all the powered digits.
- If the sum equals the original number, it is an Armstrong number.
Implementation Script
@echo off
setlocal enabledelayedexpansion
set /p "num=Enter a number: "
set "original=!num!"
:: 1. Count the digits
set "digitCount=0"
set "temp=!num!"
:count_loop
if defined temp (
set "temp=!temp:~1!"
set /a "digitCount+=1"
goto :count_loop
)
:: 2. Extract digits and calculate sum of powers
set "total=0"
set "temp=!num!"
:digit_loop
if not defined temp goto :check_result
:: Get the leftmost digit
set "d=!temp:~0,1!"
set "temp=!temp:~1!"
:: Calculate d ^ digitCount
set "power=1"
for /L %%i in (1,1,!digitCount!) do (
set /a "power*=d"
)
:: Add to total
set /a "total+=power"
goto :digit_loop
:: 3. Compare
:check_result
echo.
echo Number: !original!
echo Digit Count: !digitCount!
echo Sum of Powers: !total!
echo.
if !total! EQU !original! (
echo [RESULT] !original! IS an Armstrong number!
) else (
echo [RESULT] !original! is NOT an Armstrong number.
)
pause
The exponentiation loop for /L %%i in (1,1,!digitCount!) multiplies power by the digit d a total of digitCount times. This is equivalent to computing $d^n$ without needing a dedicated power function. For digit 5 with a count of 3, the loop computes 1 * 5 * 5 * 5 = 125.
Common Armstrong Numbers
Here are some well-known Armstrong numbers to test with:
- 1-digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (all single-digit numbers are Armstrong numbers by definition).
- 3-digit: 153, 370, 371, 407.
- 4-digit: 1634, 8208, 9474.
Important Limitations
- 32-Bit Overflow: Because we raise digits to the power of $n$, even a modest 7-digit number will involve calculations like $9^7 = 4,782,969$. Summing several such values will quickly approach the ~2.1 billion Batch limit. For numbers with more than about 8 digits, use PowerShell.
- Positive Integers Only: This check is only valid for non-negative whole numbers.
- Zero Check: The number 0 is technically an Armstrong number ($0^1 = 0$), so ensure your script handles it correctly.
User input obtained via set /p is inherently untrusted. A non-numeric value will cause set /a to fail with an error, and input containing special characters like &, |, or > can break the script or execute unintended commands. Always validate input before performing arithmetic on it.
The for /L loop in Batch evaluates its bounds once at the start of execution. If the bounds variable uses percent expansion (%digitCount%), it works correctly outside of parenthesized blocks. However, if this for /L is nested inside another block where digitCount was just computed, you must use delayed expansion (!digitCount!) to ensure the loop receives the updated value rather than a stale or empty one.
Conclusion
Checking for Armstrong numbers is a rewarding exercise in combining digit extraction, looping, and exponentiation, three key skills for advanced Batch scripting. It demonstrates how seemingly simple numerical analysis requires the coordination of multiple programming techniques. By mastering this pattern, you strengthen your ability to build complex mathematical validators and data analysis tools within the Windows command-line environment.