Java ProgramsControl FlowPrint Tables From 1 to 10

Print Tables From 1 to 10 in Java

intermediate·  Control Flow  ·  Loops

Problem

Printing one table is a single loop; printing every table from 1 to 10 means repeating that same loop once per table, which is exactly what a nested loop does.

Print the multiplication tables for every number from 1 to 10, one after another.

Input
1 to 10
Output
1 x 1 = 1 1 x 2 = 2 ... 10 x 9 = 90 10 x 10 = 100

Java Program

Java
public class AllTablesOneToTen { public static void main(String[] args) { for (int table = 1; table <= 10; table++) { for (int i = 1; i <= 10; i++) { // resets to 1 for every table System.out.println(table + " x " + i + " = " + (table * i)); } } } }

Output

1 x 1 = 1 1 x 2 = 2 ... 10 x 9 = 90 10 x 10 = 100

Core Logic

An outer loop selects which table is currently being printed, and an inner loop — a complete single-table loop on its own — runs from scratch for each one.

How It Works
  1. 1The outer for (int table = 1; table <= 10; table++) picks the current table's number, from 1 to 10.
  2. 2The inner for (int i = 1; i <= 10; i++) runs its full ten iterations every single time the outer loop advances.
  3. 3Each inner iteration prints "table x i = result", the same line format as printing a single table.
  4. 4By the time the outer loop finishes, the inner loop has run ten separate times, ten iterations each.
When table = 1, the inner loop prints 1 x 1 = 1 through 1 x 10 = 10; when table advances to 2, the inner loop restarts from i = 1 and prints 2 x 1 = 2 through 2 x 10 = 20, and so on.
💡

Key Point: The inner loop's counter i resets back to 1 every time the outer loop advances — it doesn't keep counting up across tables, it restarts fresh for each one.

Key Concepts

nested for loopouter and inner loopmultiplication

Approach 2: Java 8

Java
import java.util.stream.IntStream; public class AllTablesOneToTenStream { public static void main(String[] args) { IntStream.rangeClosed(1, 10).forEach(table -> IntStream.rangeClosed(1, 10).forEach(i -> System.out.println(table + " x " + i + " = " + (table * i)) ) ); } }

Output

1 x 1 = 1 1 x 2 = 2 ... 10 x 9 = 90 10 x 10 = 100

Core Logic

Nesting one IntStream inside another mirrors the nested loop directly — the outer stream picks the table, and a fresh inner stream runs for each one.

How It Works
  1. 1The outer IntStream.rangeClosed(1, 10) produces each table number in turn.
  2. 2.forEach(table -> ...) runs a full inner stream for every table value, just like the outer loop re-entering the inner loop.
  3. 3The inner IntStream.rangeClosed(1, 10) is built fresh inside the lambda, so it starts back at 1 for every table, exactly like the inner loop resetting.
  4. 4.forEach(i -> System.out.println(...)) prints each line of the current table.
For table = 1, the inner stream prints 1 x 1 = 1 through 1 x 10 = 10, then table advances to 2 and a brand new inner stream runs from 1 again.
💡

Key Point: The inner stream has to be created inside the outer lambda, not reused from outside it — a stream can only be consumed once, so a new one is needed for every table.

Complexity
Time Complexity: O(n * m)Space Complexity: O(1)

Why: The outer stream runs n times and spawns a fresh inner stream of m elements each time, the same total work as the nested loop, without collecting anything.

Key Concepts

Streamnested IntStream.rangeClosed()

Related Programs