Skip to main content

How to Generate a Multiplication Table in Batch Script

Generating a multiplication table in a Batch script is one of the best ways to practice using FOR /L (incrementing) loops, variable padding for alignment, and delayed expansion for building strings dynamically. It transforms simple mathematical operations into structured, visual data.

In this guide, we will write three scripts: a standard vertical list for a single multiplier, a full 2D matrix grid using nested loops, and a CSV generator to export the data.

Method 1: The Single Number Vertical Table

If you just need the standard $7 \times 1 = 7$ through $7 \times 10 = 70$ format, a single FOR /L loop is sufficient. To make the output look like a professional chart instead of a ragged list, we will use basic string padding.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set "BaseNumber=7"
set "MaxMultiplier=10"

echo Multiplication Table for %BaseNumber%
echo -----------------------------

:: FOR /L %%variable IN (start, step, end)
for /L %%i in (1, 1, %MaxMultiplier%) do (

:: Calculate the product
set /a "Result=BaseNumber * %%i"

:: Pad the multiplier to 2 spaces (e.g., " 1", "10")
set "PadMulti= %%i"
set "PadMulti=!PadMulti:~-2!"

:: Pad the result to 3 spaces (e.g., " 7", " 70")
set "PadRes= !Result!"
set "PadRes=!PadRes:~-3!"

:: Print the cleanly formatted line
echo %BaseNumber% x !PadMulti! = !PadRes!
)

echo -----------------------------
endlocal
pause
exit /b 0

Method 2: The Full Matrix Grid (Nested Loops)

To generate a full $10 \times 10$ matrix, we must construct our output row-by-row before printing it to the console (since standard echo automatically drops to an entirely new line every time it is called). We use an Outer Loop for the rows and an Inner Loop for the columns.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set "GridSize=10"

echo Multiplication Table Matrix (1 to %GridSize%^)
echo ------------------------------------------------

:: 1. Construct the Top Header Row
set "HeaderRow= |"
for /L %%C in (1, 1, %GridSize%) do (
set "PadCol= %%C"
set "HeaderRow=!HeaderRow!!PadCol:~-4!"
)
echo !HeaderRow!
echo ------------------------------------------------

:: 2. Generate the Main Grid
:: Outer loop (%%R) handles the Rows
for /L %%R in (1, 1, %GridSize%) do (

:: Start the line string with the Row Header (left side)
set "RowHeader= %%R"
set "RowData=!RowHeader:~-3! |"

:: Inner loop (%%C) handles the Columns
for /L %%C in (1, 1, %GridSize%) do (
set /a "Product=%%R * %%C"

:: Pad every cell to 4 total spaces
set "PadProd= !Product!"
set "RowData=!RowData!!PadProd:~-4!"
)

:: Finally, echo the fully constructed row
echo !RowData!
)

endlocal
pause
exit /b 0
info

By assigning values straight to RowData over and over (set RowData=!RowData! + [new cell]), we are concatenating the entire line horizontally inside memoized batch variables before writing anything to the screen.

Method 3: Exporting the Matrix to a CSV

Visual console grids are excellent for administration panels, but sometimes you need to export the tables to Excel or another data-parsing application. Instead of padding with spaces, we string columns together using a comma , delimiter.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set "GridSize=10"
set "CsvFile=multiplication_table.csv"

echo Generating CSV Matrix up to %GridSize%x%GridSize%...

:: 1. Construct and write the Header Row
set "Header="
for /L %%C in (1, 1, %GridSize%) do (
if "!Header!"=="" (
set "Header=%%C"
) else (
:: Append with a comma separator
set "Header=!Header!,%%C"
)
)
:: Write to file (note the leading comma for the empty top-left cell)
> "%CsvFile%" echo ,!Header!

:: 2. Generate and write the Data Rows
for /L %%R in (1, 1, %GridSize%) do (

:: Row start (left column header)
set "RowData=%%R"

for /L %%C in (1, 1, %GridSize%) do (
set /a "Product=%%R * %%C"
set "RowData=!RowData!,!Product!"
)

:: Append the assembled CSV row into the file
>> "%CsvFile%" echo !RowData!
)

echo [SUCCESS] Table saved to "%CsvFile%".
endlocal
pause
exit /b 0

Best Practices

  1. Mandatory Delayed Expansion: Notice how setlocal enabledelayedexpansion is called at the beginning, and all variables changed inside the for loops are read using exclamation marks (e.g., !Product!, !RowData!). If you try to use standard percent signs (%Product%), the script will fail completely because the variables would be evaluated exactly once when the loop was initially loaded, not during every mathematical cycle.
  2. Text Alignment Trick: A mathematical matrix is useless if the columns jitter left and right depending on if a number has 1, 2, or 3 digits. The string slicing formula set "padded= !num!" & set "padded=!padded:~-4!" forces variables to always take up exactly 4 column characters, effectively left-padding them with spaces.
  3. Optimizing Redirection: In the CSV script, the redirection operators (> and >>) are placed at the start of the command (e.g., > "%CsvFile%" echo ,!Header!). This prevents Batch from capturing a trailing space as part of the echoed string. A common mistake is writing echo !RowData! >> file.csv, which exports an invisible trailing space into every line and can corrupt strict parsing engines.

Conclusion

Building a multiplication table forces you to leverage the best of Batch's native FOR /L loops and string concatenation capabilities. By combining delayed expansion with substring padding, you ensure that every row generated isn't just mathematically correct, but properly formatted for human console display or CSV raw-data ingestion.