Print Multiplication Table in Java
beginner· Control Flow · Loops
Problem
A multiplication table lists a number's product with each of 1 through 10 in order, which a loop can generate one row at a time.
Given a number, print its multiplication table from 1 to 10.
Input
num = 7
Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Java Program
Java
public class MultiplicationTable {
public static void main(String[] args) {
int num = 7;
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Core Logic
Multiplying the given number by every value from 1 to 10 in turn, and printing each result as it's computed, builds the table row by row.
How It Works
- 1
for (int i = 1; i <= 10; i++)counts through the ten standard rows of a multiplication table. - 2Each iteration computes
num * iand prints it alongsidenumandiin the"num x i = result"format. - 3No result from one row is needed to compute the next — each row is independent.
For
num = 7, the loop prints ten lines, from 7 x 1 = 7 up to 7 x 10 = 70.💡
Key Point: The table always runs from 1 to 10 by convention, regardless of how large num itself is — the loop bound isn't tied to the input.
Key Concepts
for loopmultiplicationString concatenation
Approach 2: Java 8
Java
import java.util.stream.IntStream;
public class MultiplicationTableStream {
public static void main(String[] args) {
int num = 7;
IntStream.rangeClosed(1, 10).forEach(i -> System.out.println(num + " x " + i + " = " + (num * i)));
}
}
Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Core Logic
The same fixed 1-to-10 sweep can be expressed as a stream, printing each row from within the lambda.
How It Works
- 1
IntStream.rangeClosed(1, 10)produces the ten multipliers, the same fixed range the loop counts through. - 2
.forEach(i -> ...)computes and prints each row's result directly inside the lambda body.
For
num = 7, the stream produces the same ten rows the loop does, in the same order.💡
Key Point: Unlike a filter-then-collect pipeline, this stream exists purely to drive forEach() — there's no result being built up, just ten print statements executed in order.
Key Concepts
StreamIntStream.rangeClosed()forEach()