C Program to Print Multiplication Table
Use a
for loop in C to print the multiplication table by multiplying the given number with loop counters and printing the result, like for(int i=1; i<=10; i++) printf("%d x %d = %d\n", num, i, num*i);.Examples
Input5
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
Input1
Output1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
Input0
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, first get the number from the user. Then, use a loop that runs from 1 to 10. In each loop step, multiply the number by the loop counter and print the result in a readable format.
Algorithm
1
Get the number input from the user.2
Start a loop from 1 to 10.3
In each iteration, multiply the input number by the loop counter.4
Print the multiplication expression and result.5
End the loop after 10 iterations.Code
c
#include <stdio.h> int main() { int num, i; printf("Enter a number: "); scanf("%d", &num); for(i = 1; i <= 10; i++) { printf("%d x %d = %d\n", num, i, num * i); } return 0; }
Output
Enter a number: 5
5 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
Dry Run
Let's trace the program with input 3 through the code.
1
Input number
User enters 3, so num = 3
2
Start loop
i starts at 1
3
Calculate and print
Print 3 x 1 = 3
4
Increment loop
i becomes 2
5
Repeat calculation
Print 3 x 2 = 6
6
Continue until i=10
Prints up to 3 x 10 = 30
| i | num | num * i | Output line |
|---|---|---|---|
| 1 | 3 | 3 | 3 x 1 = 3 |
| 2 | 3 | 6 | 3 x 2 = 6 |
| 3 | 3 | 9 | 3 x 3 = 9 |
| 4 | 3 | 12 | 3 x 4 = 12 |
| 5 | 3 | 15 | 3 x 5 = 15 |
| 6 | 3 | 18 | 3 x 6 = 18 |
| 7 | 3 | 21 | 3 x 7 = 21 |
| 8 | 3 | 24 | 3 x 8 = 24 |
| 9 | 3 | 27 | 3 x 9 = 27 |
| 10 | 3 | 30 | 3 x 10 = 30 |
Why This Works
Step 1: Getting user input
The program uses scanf to read the number from the user and store it in num.
Step 2: Looping from 1 to 10
A for loop runs from 1 to 10, representing the multiplier in the table.
Step 3: Printing each multiplication line
Inside the loop, the program multiplies num by the loop counter i and prints the result in the format num x i = product.
Alternative Approaches
Using while loop
c
#include <stdio.h> int main() { int num, i = 1; printf("Enter a number: "); scanf("%d", &num); while(i <= 10) { printf("%d x %d = %d\n", num, i, num * i); i++; } return 0; }
This uses a while loop instead of for loop; functionally the same but shows different loop style.
Printing table for fixed number 7
c
#include <stdio.h> int main() { int i; for(i = 1; i <= 10; i++) { printf("7 x %d = %d\n", i, 7 * i); } return 0; }
This prints the table for number 7 without user input, useful for fixed tables.
Complexity: O(1) time, O(1) space
Time Complexity
The loop runs exactly 10 times, so time is constant O(1), independent of input size.
Space Complexity
Only a few variables are used, so space is constant O(1).
Which Approach is Fastest?
All approaches use simple loops with fixed iterations, so performance differences are negligible.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop with user input | O(1) | O(1) | Flexible input and clear structure |
| While loop with user input | O(1) | O(1) | Alternative loop style for beginners |
| Fixed number without input | O(1) | O(1) | Quick fixed table printing |
Always initialize your loop counter and clearly format the output for easy reading.
Beginners often forget to increment the loop counter, causing an infinite loop.