Python Program to Print Multiplication Table
You can print a multiplication table in Python using a
for loop like this: for i in range(1, 11): print(f"{num} x {i} = {num * i}") where num is the number you want the table for.Examples
Inputnum = 2
Output2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Inputnum = 5
Output5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Inputnum = 0
Output0 x 1 = 0
0 x 2 = 0
0 x 3 = 0
0 x 4 = 0
0 x 5 = 0
0 x 6 = 0
0 x 7 = 0
0 x 8 = 0
0 x 9 = 0
0 x 10 = 0
How to Think About It
To print a multiplication table, think of counting from 1 to 10 and multiplying each count by the chosen number. For each step, you show the number, the multiplier, and the result. This repeats until you reach 10.
Algorithm
1
Get the number for which the multiplication table is needed.2
Start a loop from 1 to 10.3
Multiply the number by the current loop value.4
Print the multiplication expression and result.5
Repeat until the loop reaches 10.Code
python
num = int(input("Enter a number: ")) for i in range(1, 11): print(f"{num} x {i} = {num * i}")
Output
Enter a number: 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Dry Run
Let's trace the multiplication table for num = 3 through the code
1
Input number
num = 3
2
Start loop from 1 to 10
i = 1
3
Calculate and print
3 x 1 = 3
4
Next iteration
i = 2, print 3 x 2 = 6
5
Continue until i = 10
Print 3 x 10 = 30
| i | Expression | Result |
|---|---|---|
| 1 | 3 x 1 | 3 |
| 2 | 3 x 2 | 6 |
| 3 | 3 x 3 | 9 |
| 4 | 3 x 4 | 12 |
| 5 | 3 x 5 | 15 |
| 6 | 3 x 6 | 18 |
| 7 | 3 x 7 | 21 |
| 8 | 3 x 8 | 24 |
| 9 | 3 x 9 | 27 |
| 10 | 3 x 10 | 30 |
Why This Works
Step 1: Input the number
We get the number from the user to know which multiplication table to print.
Step 2: Loop from 1 to 10
The loop runs 10 times because multiplication tables usually go from 1 to 10.
Step 3: Print each multiplication step
For each loop value, we multiply it by the number and print the result in a clear format.
Alternative Approaches
Using while loop
python
num = int(input("Enter a number: ")) i = 1 while i <= 10: print(f"{num} x {i} = {num * i}") i += 1
This uses a while loop instead of for loop; both work but for loop is simpler here.
Using list comprehension and join
python
num = int(input("Enter a number: ")) print('\n'.join([f"{num} x {i} = {num * i}" for i in range(1, 11)]))
This creates all lines first and prints them at once; good for compact code but less step-by-step.
Complexity: O(1) time, O(1) space
Time Complexity
The loop runs exactly 10 times, so the time is constant, O(1), regardless of input size.
Space Complexity
No extra space grows with input; only a few variables are used, so space is O(1).
Which Approach is Fastest?
All approaches run in constant time; using a for loop is simplest and most readable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop | O(1) | O(1) | Simple and clear code |
| While loop | O(1) | O(1) | When loop condition varies |
| List comprehension | O(1) | O(1) | Compact code, printing all at once |
Use a for loop with range(1, 11) to easily print multiplication tables from 1 to 10.
Beginners often forget that range(1, 11) stops before 11, so the table only goes up to 10.