Skip to content

How to Check if a Number is an Armstrong Number in Batch Script

Learn how to write a Windows Batch script to verify if a number is an Armstrong (Narcissistic) number by extracting digits, raising them to the power of digit count, and comparing the sum to the original number.

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

  1. Count the number of digits (n).
  2. Extract each digit.
  3. Raise each digit to the power of n.
  4. Sum all the powered digits.
  5. 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

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

  1. 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.
  2. Positive Integers Only: This check is only valid for non-negative whole numbers.
  3. Zero Check: The number 0 is technically an Armstrong number (0^1 = 0), so ensure your script handles it correctly.

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.

Navigation