Java Program to Print Multiplication Table
You can print a multiplication table in Java using a
for loop like this: for (int i = 1; i <= 10; i++) { System.out.println(number + " x " + i + " = " + (number * i)); } where number is the base number for the table.Examples
Inputnumber = 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
Inputnumber = 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
Inputnumber = 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 to print the multiplication table.2
Start a loop from 1 to 10.3
Multiply the number by the current loop count.4
Print the multiplication expression and result.5
Repeat until the loop reaches 10.Code
java
public class MultiplicationTable { public static void main(String[] args) { int number = 5; // Change this to print a different table for (int i = 1; i <= 10; i++) { System.out.println(number + " x " + i + " = " + (number * i)); } } }
Output
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 multiplication table for number = 3 through the code
1
Set number
number = 3
2
Start loop
i = 1
3
Calculate and print
3 x 1 = 3
4
Increment loop
i = 2
5
Calculate and print
3 x 2 = 6
| i | number * i | Output |
|---|---|---|
| 1 | 3 | 3 x 1 = 3 |
| 2 | 6 | 3 x 2 = 6 |
| 3 | 9 | 3 x 3 = 9 |
| 4 | 12 | 3 x 4 = 12 |
| 5 | 15 | 3 x 5 = 15 |
| 6 | 18 | 3 x 6 = 18 |
| 7 | 21 | 3 x 7 = 21 |
| 8 | 24 | 3 x 8 = 24 |
| 9 | 27 | 3 x 9 = 27 |
| 10 | 30 | 3 x 10 = 30 |
Why This Works
Step 1: Loop from 1 to 10
The for loop runs 10 times, each time increasing the multiplier by 1.
Step 2: Multiply and print
Inside the loop, the program multiplies the base number by the current multiplier and prints the result.
Step 3: Repeat until 10
This repeats until the multiplier reaches 10, printing the full multiplication table.
Alternative Approaches
Using while loop
java
public class MultiplicationTable { public static void main(String[] args) { int number = 5; int i = 1; while (i <= 10) { System.out.println(number + " x " + i + " = " + (number * i)); i++; } } }
This uses a <code>while</code> loop instead of <code>for</code>, which is more flexible but slightly longer.
Using recursion
java
public class MultiplicationTable { public static void printTable(int number, int i) { if (i > 10) return; System.out.println(number + " x " + i + " = " + (number * i)); printTable(number, i + 1); } public static void main(String[] args) { printTable(5, 1); } }
This uses recursion to print the table, which is elegant but less common for simple loops.
Complexity: O(1) time, O(1) space
Time Complexity
The loop runs exactly 10 times, so the time is constant, O(1), regardless of the number.
Space Complexity
Only a few variables are used, so space is constant, O(1).
Which Approach is Fastest?
All approaches run in constant time; the 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) | Flexible looping |
| Recursion | O(1) | O(1) | Elegant but less common for this task |
Use a
for loop from 1 to 10 to print multiplication tables easily.Beginners often forget to update the loop counter, causing infinite loops.