Skip to main content

How to Calculate the Factorial of a Number in Batch Script

The factorial of a non-negative integer (n!) is the product of all positive integers up to that number (e.g., 5! = 5 * 4 * 3 * 2 * 1 = 120). This is a classic mathematical function and a common programming exercise. While Windows Batch has no built-in FACTORIAL() function, it can be calculated using a simple loop and the built-in arithmetic engine.

This guide will teach you how to use a FOR /L loop and the SET /A command to calculate factorials. More importantly, it will highlight the critical 32-bit integer limitation of batch's native math and show you the superior modern approach using a PowerShell one-liner, which is necessary for calculating the factorial of larger numbers.

What is a Factorial?

The factorial, denoted by an exclamation mark (!), is a product of a sequence of descending numbers.

  • 1! = 1
  • 4! = 4 * 3 * 2 * 1 = 24
  • 7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
  • By definition, 0! = 1.

The Core Method (Pure Batch): The FOR /L Loop

The most efficient "pure-batch" way to calculate a factorial is to use a FOR /L loop to generate the sequence of numbers and SET /A to multiply them together.

The logic:

  1. Initialize a result variable to 1.
  2. Use a FOR /L loop to count from 1 up to the target number.
  3. In each iteration, multiply the result by the current loop number.

The biggest problem with the pure-batch method is that SET /A can only handle 32-bit integers, meaning it will fail for factorials of numbers larger than 12. PowerShell uses 64-bit (and larger) numbers, making it the correct tool for this task.

Syntax:powershell -Command "$n=%Number%; $r=1; 1..$n | ForEach-Object { $r *= $_ }; $r"

This one-liner does the same logic as the batch loop but can handle much larger results.

Basic Example: Calculating 5!

Let's calculate the factorial of 5 (120) using both methods.

Method 1: Pure Batch Script

@ECHO OFF
SET "Number=5"
SET /A "Result=1"

REM Loop from 1 to 5
FOR /L %%i IN (1,1,%Number%) DO (
SET /A "Result *= %%i"
)

ECHO Batch Method: %Number%! = %Result%

Method 2: PowerShell Script

@ECHO OFF
SET "Number=5"
SET "Result="

FOR /F %%R IN (
'powershell -Command "$n=%Number%; $r=1; 1..$n | ForEach-Object { $r *= $_ }; $r"'
) DO (
SET "Result=%%R"
)

ECHO PowerShell Method: %Number%! = %Result%

Output (for both methods)

Batch Method: 5! = 120
PowerShell Method: 5! = 120

How the Batch Script Works

  • SET /A "Result=1": We initialize our result variable to 1.
  • FOR /L %%i IN (1,1,%Number%) DO: This creates a loop where the variable %%i will take on the values 1, 2, 3, 4, and 5.
  • SET /A "Result *= %%i": This is the core of the calculation. It is shorthand for SET /A "Result = Result * %%i". In each iteration, it multiplies the current Result by the current loop number, accumulating the product.

CRITICAL: The 32-bit Integer Limit

The SET /A command in batch uses 32-bit signed integers. The maximum value it can hold is 2,147,483,647. Factorials grow incredibly fast, and this limit is reached very quickly.

  • 12! = 479,001,600 (This works)
  • 13! = 6,227,020,800 (This is larger than the 32-bit limit)

An example of error: let's see what happens when we try to calculate 13! with the pure-batch script.

SET "Number=13"
...

Incorrect Output:

Batch Method: 13! = 1932053504

This number is completely wrong. It's the result of an integer overflow, where the calculation "wrapped around" the 32-bit limit.

Solution: For any number larger than 12, you must use the PowerShell method. PowerShell can handle these large numbers correctly.

Practical Example: An Interactive Factorial Calculator

This script prompts the user for a number, validates it, and then uses the batch method for small numbers and informs the user to use a better tool for large numbers.

@ECHO OFF
SETLOCAL

:Prompt
SET "UserInput="
ECHO.
SET /P "UserInput=Enter a number between 0 and 12 to calculate its factorial: "

REM --- Input Validation ---
REM Check if it's a number
SET /A "Num=%UserInput%" 2>NUL
IF %ERRORLEVEL% NEQ 0 (ECHO Invalid input. Please enter a number. & GOTO :Prompt)
REM Check if it's in the valid range for batch
IF %Num% LSS 0 (ECHO Cannot calculate factorial for a negative number. & GOTO :Prompt)
IF %Num% GTR 12 (ECHO That number is too large for batch. The result would be incorrect. & GOTO :Prompt)

REM --- Calculation ---
SET /A "Result=1"
IF %Num% GTR 0 (
FOR /L %%i IN (1,1,%Num%) DO (
SET /A "Result *= %%i"
)
)

ECHO.
ECHO [SUCCESS] %Num%! = %Result%

ENDLOCAL

Conclusion

While you can calculate factorials in a pure batch script, it's a perfect example of understanding the tool's limitations.

  • The FOR /L loop with SET /A is the standard "pure-batch" method and works perfectly well for small numbers.
  • This method is strictly limited to numbers up to 12! due to the 32-bit integer limit of SET /A.
  • For any number larger than 12, or for any script where reliability with large numbers is required, the PowerShell method is the only correct and recommended solution.