1. Outer loop.i is the row number and the left factor in every product.
2. Inner loop.j runs to i, so row i has exactly i values.
3. Print and break.print((i * j) + " ") builds the row; println() starts the next line.
Example 2 — Scanner Input
Read rows at runtime. Same nested-loop logic.
Java
import java.util.Scanner;
public class MultiplicationNumberTriangleInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
if (rows < 1) return;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((i * j) + " ");
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 5)
Enter the number of rows: 5
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
How It Works
1. Prompt and guard. Read rows; exit early if it is less than 1.
2. Same loops. Only the source of rows changes — the i*j logic is identical.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — No Trailing Space
Same i * j logic — space only before the second and later values on each row.
Java
public class MultiplicationNumberTriangleNoTrail {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
if (j > 1) System.out.print(" ");
System.out.print(i * j);
}
System.out.println();
}
}
}
Output
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
How It Works
1. Same products. Still print i * j for j = 1..i.
2. Smarter spacing.if (j > 1) adds a space before later values — no trailing space after the last number.
3. Same shape. The triangle looks identical; only the end-of-row whitespace is cleaner.
Edge Cases & Pitfalls
Check these before calling the solution done.
Missing println
One long line
Without println() after the inner loop, every value prints on a single horizontal line.
j <= rows
Full table
Using j <= rows instead of j <= i prints a square multiplication table, not a triangle.
rows = 1
Single value
Output is just 1.
rows = 0
Empty output
The outer loop never runs. Guard interactive input with rows >= 1.
Wrong product
Print j only
Printing j alone gives 1, 1 2, 1 2 3 — not the multiplication triangle.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
All three examples
O(n²)
O(1)
Total printed values for n rows: 1 + 2 + … + n = n(n+1)/2.
Remember
Key Takeaways
Rule: outer i=1..rows, inner j=1..i, print i*j.
Shape: each row is one longer — linked to the i-times table.
Spacing: use print for cells and one println per row.
Complexity:O(n²) prints for n rows.
One line: for each row i, print i×1 … i×i, then start a new line.
Frequently Asked Questions
The inner loop runs from j=1 to j=i. As i increases, each row prints one extra value.
Each printed number is the product i×j. Row 4 prints 4×1, 4×2, 4×3, 4×4 → 4 8 12 16.
For i=4, the inner loop uses j=1..4, so you print 4×1, 4×2, 4×3, 4×4.
Store the row count in rows and use for (int i=1; i<=rows; i++) in the outer loop.
Print a space before numbers starting from the second value, or build the row with StringBuilder — see Example 3.
Yes. Change the inner loop to for (int j=1; j<=rows; j++) so every row has rows columns.
O(n²) for n rows. Total prints are 1+2+...+n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
🤔
Did you know?
Row i prints i×1, i×2, …, i×i. Each row has one more value than the row above — a classic nested-loop exercise linked to multiplication tables.