Multiplication Table: flowchart, algorithm and program
This program reads a number n and prints its multiplication table from 1 to 10. It uses a for loop where i goes from 1 to 10, and each time it prints n x i along with the product n * i.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read n.
- Step 3: Set i = 1.
- Step 4: If i <= 10, go to step 5; otherwise go to step 7.
- Step 5: Print n x i = n * i.
- Step 6: Set i = i + 1. Go to step 4.
- Step 7: Stop.
Pseudocode
BEGIN
READ n
i ← 1
WHILE i <= 10 DO
PRINT n x i = n * i
i ← i + 1
END WHILE
END
Program in C, Python and Java
Multiplication Table program in C
#include <stdio.h>
int main() {
int n, i;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}Multiplication Table program in Python
n = int(input("Enter n: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)Multiplication Table program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
sc.close();
}
}
Sample input: n = 5
Output: Enter n: 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
How it works
- scanf reads the number into n, for example n = 5.
- The loop starts with i = 1 and keeps running while i <= 10, so it runs exactly 10 times.
- Inside the loop, printf shows n, i and the product n * i in the form '5 x 1 = 5'.
- After each print, i++ increases i by 1, so the next line uses the next multiplier.
- When i becomes 11, the condition i <= 10 is false and the loop ends, so the table stops at 10.
Dry run (trace table)
| Step | Line | Box | n | i | Output |
|---|---|---|---|---|---|
| 1 | 3 | Start | |||
| 2 | 6 | Read n | 5 | Enter n: | |
| 3 | 7 | i = 1 | 5 | 1 | |
| 4 | 7 | i <= 10? | 5 | 1 | |
| 5 | 8 | Print n x i = n * i | 5 | 1 | 5 x 1 = 5 |
| 6 | 7 | i = i + 1 | 5 | 2 | |
| 7 | 7 | i <= 10? | 5 | 2 | |
| 8 | 8 | Print n x i = n * i | 5 | 2 | 5 x 2 = 10 |
| 9 | 7 | i = i + 1 | 5 | 3 | |
| 10 | 7 | i <= 10? | 5 | 3 | |
| 11 | 8 | Print n x i = n * i | 5 | 3 | 5 x 3 = 15 |
| 12 | 7 | i = i + 1 | 5 | 4 | |
| 13 | 7 | i <= 10? | 5 | 4 | |
| 14 | 8 | Print n x i = n * i | 5 | 4 | 5 x 4 = 20 |
| 15 | 7 | i = i + 1 | 5 | 5 | |
| 16 | 7 | i <= 10? | 5 | 5 | |
| 17 | 8 | Print n x i = n * i | 5 | 5 | 5 x 5 = 25 |
| 18 | 7 | i = i + 1 | 5 | 6 | |
| 19 | 7 | i <= 10? | 5 | 6 | |
| 20 | 8 | Print n x i = n * i | 5 | 6 | 5 x 6 = 30 |
Time and space complexity
Time: O(1) · Space: O(1). The loop always runs exactly 10 times with constant work per iteration, regardless of n.
Common mistakes
- Writing i < 10 instead of i <= 10, which leaves out the last line (n x 10).
- Forgetting the & in scanf("%d", &n), so the input is not stored in n.
- Starting i at 0 and printing a first line of n x 0 = 0, or forgetting the \n so all the lines run together.
Viva questions
Q1. What does this program do?
It reads an integer n from the user and prints its multiplication table from n x 1 up to n x 10, one line per multiple.
Q2. Why does the loop variable i start at 1 and not 0?
A multiplication table starts at n x 1. If i started at 0, the first line would be n x 0 = 0, which is not part of the table.
Q3. What does the condition i <= 10 check, and what would happen if you wrote i < 10?
It keeps the loop running while i is 10 or less, so the last line printed is n x 10. With i < 10 the loop would stop at 9 and the n x 10 line would be missing.
Q4. Why do you write &n in scanf but plain n in printf?
scanf needs the address of n so it can store the typed value there. printf only needs the value of n to print it.
Q5. What output do you get if the user enters 0 or a negative number such as -3?
For 0 every line shows 0. For -3 the program works normally and prints -3, -6, -9, and so on, because the multiplication is valid for negative numbers. It does not check the input.
Q6. Why use a for loop here instead of a while loop?
The number of repetitions is known in advance (10), and a for loop puts the start, the condition and the increment on one line. A while loop would work too, but it needs i to be set and incremented separately.
Q7. What is the time complexity of this program?
The loop always runs exactly 10 times, so it is O(1), constant time. If the limit were a variable m, it would be O(m).
Q8. What would you change to let the user choose how far the table goes, for example up to 20?
Read a second integer, say limit, with scanf and change the condition to i <= limit. Nothing else in the loop needs to change.
Q9. What happens if the user types a letter instead of a number?
scanf fails and leaves n uninitialised, so the output is garbage or undefined. You can check the return value of scanf, which should be 1 for a successful read, to guard against this.
Frequently asked questions
How does the multiplication table program work in C?
A for loop runs i from 1 to 10. On each pass it prints n, i and n * i, which gives one line of the table.
How can I print the table up to a different limit, like 12 or 20?
Change the loop condition from i <= 10 to i <= 12 (or any limit). You can also read the limit from the user and use it in the condition.
Why do we use a for loop here instead of writing 10 printf statements?
The same step repeats 10 times with only i changing, so a loop is shorter and easier to change. It also avoids copy-paste mistakes.