Shape Rule
i × j
Row i prints products i×1 through i×i.

The multiplication triangle prints row i as i×1, i×2, …, i×i — e.g. 1, 2 4, 3 6 9. This tutorial covers nested-loop logic, live preview, worked Java examples, edge cases, and O(n²) complexity.
i × j
Row i prints products i×1 through i×i.
i = 1..rows
Outer loop picks row i; inner loop runs j = 1..i.
print(i*j)
Each cell is i * j — standard int multiplication for lab sizes.
Times tables
Each row shows the first i multiples of i.
3–12 rows
Pick a row count and draw the multiplication triangle instantly in the browser.
Complexity
Total prints ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).
A multiplication number triangle pattern builds row i with products i×1, i×2, …, i×i. Each row has one more value than the row above.
In Java you use nested loops: outer for (i = 1; i <= rows; i++), inner for (j = 1; j <= i; j++), print i*j, then println() after each row.
It is a classic nested-loop exercise that connects pattern printing with multiplication tables.
i = 1..rows picks each row number.
j = 1..i prints i×j each row.
Outer row + inner products — classic two-loop pattern.
Follow Program 48 powers of 11; continue to Program 50 decreasing-increasing pattern.
In short: outer i=1..rows, inner j=1..i, print i*j, then println().
Given rows = 10, print ten lines: 1, 2 4, 3 6 9, … up to row 10.
// rows = 4 (conceptual output)
// 1
// 2 4
// 3 6 9
// 4 8 12 16 | Item | Type | Description |
|---|---|---|
rows | int | How many lines to print (typically ≥ 1). |
i, j | int | Row index i and column index j; cell value is i*j. |
| Printed output | text | i values on row i — triangle shape. |
for i from 1 to rows:
for j from 1 to i:
print i * j
newline | Approach | Idea | Best for |
|---|---|---|
| Nested i×j | print(i * j) inside inner loop | Standard triangle — inner bound j<=i |
| Scanner input | sc.nextInt() for rows | User-chosen row count |
| No trailing space | Print space only before 2nd+ values | Cleaner row formatting — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | int rows = 10; |
| Outer loop | for (i = 1; i <= rows; i++) |
| Inner loop | for (j = 1; j <= i; j++) |
| Print cell | System.out.print((i * j) + " "); |
| Row break | System.out.println(); after inner loop |
| Program 48 contrast | Powers of 11 uses one loop; this pattern uses nested loops with i×j products |
How outer row selection, inner products, and row breaks work together.
for (i = 1; i <= rows; i++)Picks row number i — triangle height.
for (j = 1; j <= i; j++)Prints exactly i products on row i.
i * jEach value is the product of row and column indices.
trace i=4Dry-run row 4: j=1..4 → prints 4 8 12 16.
Reach for this pattern when teaching nested loops, multiplication tables, and growing inner bounds.
Classic follow-up after single-loop series patterns like powers of 11.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 48 (powers of 11), then continue to Program 50 (decreasing-increasing pattern).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in loop variables, running totals, and O(n²) thinking.
Choose a row count and draw the multiplication triangle pattern in the browser.
Three complete Java programs — fixed rows = 10, Scanner input, and a no-trailing-space variant. Click View Output to reveal sample console results.
Print ten rows with nested loops and i*j products.
rows = 10Hard-coded size — outer loop for rows, inner loop prints i*j.
public class MultiplicationNumberTriangle {
public static void main(String[] args) {
int rows = 10;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((i * j) + " ");
}
System.out.println();
}
}
} When i = 3, the inner loop runs j = 1, 2, 3 and prints 3 6 9. Each row has exactly i values.
Read rows with Scanner for flexible output size.
Same nested loops; row count comes from user input.
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();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((i * j) + " ");
}
System.out.println();
}
sc.close();
}
} Identical loop structure to Example 1; only the row count is dynamic.
Avoid trailing spaces by printing a space only before the second and later values.
Print a leading space only when j > 1.
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();
}
}
} Same i*j logic; spacing is cleaner without a trailing space after the last value on each row.
System.out is built in; use Scanner when reading input. Set rows (e.g. 10).
for (i = 1; i <= rows; i++) — one iteration per output line.
for (j = 1; j <= i; j++) prints i*j with spaces, building each row.
System.out.println(); after the inner loop moves to the next row.
Total prints ≈ n(n+1)/2 — O(n²) time, O(1) extra memory.
i = 4Trace row 4 to see how the inner loop builds 4 8 12 16.
| j | i × j | Row so far |
|---|---|---|
| 1 | 4×1 = 4 | 4 |
| 2 | 4×2 = 8 | 4 8 |
| 3 | 4×3 = 12 | 4 8 12 |
| 4 | 4×4 = 16 | 4 8 12 16 |
After the inner loop, println() moves to the next row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use Scanner for dynamic row count — see Example 2.
Each row shows the first i multiples of i — direct link to times tables.
Example: compare row 5 (5 10 15 20 25) with the 5-times table.
Practice println vs print for multi-line vs single-line output.
Example: put System.out.print(res + " ") for one-line output — Example 3.
Change inner bound to j <= rows to print a rectangular multiplication table.
Example: print rows=5 with j<=rows and compare triangle vs table shape.
One loop iteration per row makes O(n²) concrete for beginners.
Example: count values for rows=5 → 1+2+3+4+5 = 15 printed numbers.
Pair the pattern with Scanner and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Java courses.
Wrong inner bound (j <= rows on every row) breaks the triangle shape.
Only loops and console output — no arrays or math libraries.
Change rows, use Scanner, or remove trailing spaces with conditional print.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the nested loops first; then try Scanner input and the no-trailing-space variant in Example 3.
Small habits that keep number-pattern code clean.
Use rows for height and i/j for row/column indices.
ScannerAvoid crashes when the user types letters instead of a number.
Call println() after the inner loop finishes — not inside it.
Use print(i*j + " ") inside the inner loop; one println() per row.
Trace rows = 4, i = 3, j = 2 on paper before coding larger demos.
Pro Tip: if each row has the same number of values, check whether the inner loop ends at rows instead of i.
Mistakes that commonly break multiplication number triangle patterns.
Each product lands on its own line — you get a column, not a triangle row.
→ Use print inside the inner loop; println() only after it finishes.
Every row prints rows values — you get a rectangle, not a triangle.
→ Keep for (j = 1; j <= i; j++) for the triangle shape.
Omitting println() after the inner loop glues all rows onto one line.
→ Always call System.out.println() after the inner loop completes.
Using j*i is fine mathematically, but confusing bounds break the intended row layout.
→ Keep outer i for rows and inner j from 1 to i.
Letters or empty input throw InputMismatchException.
→ Use sc.hasNextInt() before sc.nextInt().
Using literal 10 in loop bounds instead of variable rows breaks dynamic input.
→ Use one rows variable for the outer loop bound.
Check these inputs before calling the solution done.
Output is one line: 1.
Loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Large row counts produce many values — fine for labs; use smaller n for quick demos.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
Use Print space only before 2nd+ values for one horizontal line — see Example 3.
Try these variations to lock in the pattern.
rows = 3, 6, or 12j <= rowsi*j with i+jn(n+1)/2 (e.g. 15 values for rows=5).print stays on the line; println advances — mix them carefully.rows > 0 for interactive programs; rows = 1 prints one value.j <= i for the triangle — not a fixed width.Quick Takeaway: outer i=1..rows, inner j=1..i, print(i*j), then println().
| Program | Time | Extra space |
|---|---|---|
| Fixed rows = 10 (Example 1) | O(n²) | O(1) |
| Scanner input (Example 2) | O(n²) | O(1) |
| No trailing space (Example 3) | O(n²) | O(1) |
The multiplication triangle combines nested loops with i×j products — a natural step after powers-of-11 patterns. Master the fixed-rows version first, then try Scanner input and the no-trailing-space variant in Example 3.
Practice the three examples above, then continue to Program 50 for the decreasing-increasing number pattern (12345, 21234…).
Keep println() after the inner loop — one row break per outer iteration.
i, inner j=1..i, and i*j before codingprint(i*j + " ") and println() after the inner looprows ≥ 1 for interactive programsScanner return value before using rowsprintln() inside the inner loop (one value per line)j <= rows when triangle shape is required (j <= i)rowsrows = 1 edge casePrint the pattern the beginner-friendly way.
Each cell prints i*j
i = 1..rows
Codeprint(i * j)
Logicn(n+1)/2 prints
O(n²)
AnalysisRow 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.
Move on to the decreasing-increasing number pattern in the Java number-pattern series.
12 people found this page helpful