0
0
PythonProgramBeginner · 2 min read

Python Program to Print Multiplication Table Pattern

Use nested for loops in Python to print a multiplication table pattern, for example: for i in range(1, n+1): for j in range(1, n+1): print(i*j, end='\t') print().
📋

Examples

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

How to Think About It

To print a multiplication table pattern, think of rows and columns like a grid. Each row number multiplies by each column number. Use two loops: the outer loop for rows and the inner loop for columns. Multiply the current row and column numbers and print the result in a formatted way.
📐

Algorithm

1
Get the input number n for the size of the table.
2
Start an outer loop from 1 to n for rows.
3
Inside the outer loop, start an inner loop from 1 to n for columns.
4
Multiply the current row and column numbers and print the product with a tab space.
5
After the inner loop ends, print a new line to start the next row.
6
Repeat until all rows are printed.
💻

Code

python
n = int(input('Enter the size of multiplication table: '))
for i in range(1, n + 1):
    for j in range(1, n + 1):
        print(i * j, end='\t')
    print()
Output
Enter the size of multiplication table: 5 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
🔍

Dry Run

Let's trace the program with n = 3 to see how it prints the multiplication table.

1

Input

User inputs n = 3

2

Outer loop i=1

Inner loop runs j=1 to 3, prints 1*1=1, 1*2=2, 1*3=3

3

Outer loop i=2

Inner loop runs j=1 to 3, prints 2*1=2, 2*2=4, 2*3=6

4

Outer loop i=3

Inner loop runs j=1 to 3, prints 3*1=3, 3*2=6, 3*3=9

iji*j
111
122
133
212
224
236
313
326
339
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, each iteration representing a row in the table.

Step 2: Inner loop controls columns

The inner for loop runs from 1 to n, each iteration representing a column in the current row.

Step 3: Print product with tab

Inside the inner loop, multiply the row and column numbers and print the result with a tab to align columns.

Step 4: New line after each row

After finishing each row, print a newline to start the next row on a new line.

🔄

Alternative Approaches

Using list comprehension and join
python
n = int(input('Enter size: '))
for i in range(1, n+1):
    print('\t'.join(str(i*j) for j in range(1, n+1)))
This method uses list comprehension for cleaner code but may be less clear for beginners.
Using while loops
python
n = int(input('Enter size: '))
i = 1
while i <= n:
    j = 1
    while j <= n:
        print(i*j, end='\t')
        j += 1
    print()
    i += 1
This uses while loops instead of for loops, which is more manual but shows loop control explicitly.

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

Time Complexity

The program uses two nested loops each running n times, so it performs n*n = n^2 multiplications and prints.

Space Complexity

The program uses constant extra space, only variables for loop counters and no additional data structures.

Which Approach is Fastest?

All approaches have the same time complexity O(n^2). Using list comprehension may be slightly slower due to string joining overhead but is more concise.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Clarity and simplicity
List comprehension with joinO(n^2)O(1)Concise code
While loopsO(n^2)O(1)Explicit loop control
💡
Use \t to align columns neatly in the multiplication table output.
⚠️
Beginners often forget to print a newline after each row, causing all output to appear on one line.