0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Print Multiplication Table

Use a nested for loop in PowerShell like for ($i=1; $i -le 10; $i++) { for ($j=1; $j -le 10; $j++) { "$i * $j = $($i * $j)" } } to print the multiplication table.
📋

Examples

Input1 to 3
Output1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
Input1 to 5
Output1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
Input1 to 1
Output1 * 1 = 1
🧠

How to Think About It

To print a multiplication table, think of two numbers that multiply together. Use one loop to go through the first number and a second loop inside it for the second number. For each pair, multiply and print the result.
📐

Algorithm

1
Set the maximum number for the table (e.g., 10).
2
Start the first loop from 1 to the maximum number.
3
Inside the first loop, start a second loop from 1 to the maximum number.
4
Multiply the current numbers from both loops.
5
Print the multiplication expression and result.
6
Repeat until all pairs are printed.
💻

Code

powershell
for ($i = 1; $i -le 10; $i++) {
    for ($j = 1; $j -le 10; $j++) {
        Write-Output "$i * $j = $($i * $j)"
    }
}
Output
1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 1 * 10 = 10 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 80 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90 10 * 1 = 10 10 * 2 = 20 10 * 3 = 30 10 * 4 = 40 10 * 5 = 50 10 * 6 = 60 10 * 7 = 70 10 * 8 = 80 10 * 9 = 90 10 * 10 = 100
🔍

Dry Run

Let's trace the multiplication table for 1 to 3 through the code

1

Outer loop starts with i=1

i=1

2

Inner loop runs j=1 to 3

j=1, print '1 * 1 = 1'; j=2, print '1 * 2 = 2'; j=3, print '1 * 3 = 3'

3

Outer loop increments to i=2 and inner loop repeats

i=2; j=1, print '2 * 1 = 2'; j=2, print '2 * 2 = 4'; j=3, print '2 * 3 = 6'

ijOutput
111 * 1 = 1
121 * 2 = 2
131 * 3 = 3
212 * 1 = 2
222 * 2 = 4
232 * 3 = 6
313 * 1 = 3
323 * 2 = 6
333 * 3 = 9
💡

Why This Works

Step 1: Outer loop controls the first number

The for loop with variable $i runs from 1 to 10, representing the first number in the multiplication.

Step 2: Inner loop controls the second number

Inside the outer loop, another for loop with variable $j runs from 1 to 10, representing the second number.

Step 3: Print the multiplication result

For each pair ($i, $j), the script calculates $i * $j and prints it in the format i * j = result.

🔄

Alternative Approaches

Using While Loops
powershell
 $i = 1
while ($i -le 10) {
    $j = 1
    while ($j -le 10) {
        Write-Output "$i * $j = $($i * $j)"
        $j++
    }
    $i++
}
While loops achieve the same result but are less concise than for loops.
Formatted Table Output
powershell
for ($i = 1; $i -le 10; $i++) {
    $line = ""
    for ($j = 1; $j -le 10; $j++) {
        $line += "{0,4}" -f ($i * $j)
    }
    Write-Output $line
}
Prints a clean grid of numbers instead of individual lines, better for visualizing the table.
Function with Parameter
powershell
function Print-MultiplicationTable($max) {
    for ($i = 1; $i -le $max; $i++) {
        for ($j = 1; $j -le $max; $j++) {
            Write-Output "$i * $j = $($i * $j)"
        }
    }
}
Print-MultiplicationTable -max 5
Encapsulates logic in a function and allows dynamic table size.

Complexity: O(n^2) time, O(1) space

Time Complexity

The script uses two nested loops each running up to n times, resulting in O(n^2) operations to print all multiplication pairs.

Space Complexity

The script uses constant extra space, only storing loop counters and temporary strings, so O(1) space.

Which Approach is Fastest?

All approaches have similar time complexity; using for loops is concise and clear, while formatted output trades some speed for readability.

ApproachTimeSpaceBest For
For LoopsO(n^2)O(1)Simple and clear multiplication tables
While LoopsO(n^2)O(1)Same as for loops but less concise
Formatted Table OutputO(n^2)O(1)Better visual layout for tables
Function with ParameterO(n^2)O(1)Reusable and dynamic table size
💡
Use nested loops in PowerShell to generate pairs of numbers for multiplication.
⚠️
Beginners often forget to use nested loops and try to multiply numbers without pairing each with all others.