Row Start
res = i
First value on row i is always i when i == j.

The diagonal-fill triangle starts each row with index i, then generates remaining values using res = res + k where k begins at rows - 1 and decreases. For rows = 5: 1, 2 6, 3 7 10, 4 8 11 13, 5 9 12 14 15. This tutorial covers the step logic, live preview, worked Java examples, edge cases, and O(n²) complexity.
res = i
First value on row i is always i when i == j.
k = rows-1
k starts at rows-1 each row and decreases after each addition step.
res + k
After the first value, update res = res + k and print res; then k--.
i values
Row i prints exactly i numbers — inner loop j = i..i+i-1.
3–12 for n
Pick size n and draw the full diagonal-fill triangle pattern instantly in the browser.
Complexity
Total prints = n(n+1)/2 — triangular growth, O(n²) time.
A diagonal-fill number triangle starts each row with i and builds the rest using decreasing step sizes. Row 2 becomes 2 6; row 3 becomes 3 7 10.
In Java: outer for (i = 1; i <= rows; i++), set k = rows - 1 and res = i, inner for (j = i; j < i + i; j++) — print j when i == j, else res = res + k, then println().
It shows how a running total and a shrinking step fill a triangle without a 2D array — the same numbers as a column-wise fill, generated row by row.
First value on row i is i when i == j.
kStarts at rows - 1 each row, then k--.
After the first value: res = res + k.
Follow Program 54; continue to Program 56 next.
In short: outer i = 1..rows; set k = rows - 1 and res = i; inner j = i..i+i-1 prints j or res + k, then println().
Given rows = 5, print five lines with 1, 2, 3, 4, and 5 numbers respectively.
// rows = 5 (conceptual output)
// 1
// 2 6
// 3 7 10
// 4 8 11 13
// 5 9 12 14 15 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle rows (typically ≥ 1). |
i, j, k, res | int | Row i; inner index j; step k; running total res. |
| Printed output | text | i numbers on row i; total rows(rows+1)/2 values. |
for i from 1 to rows:
k = rows-1; res = i
for j from i to i+i-1:
if i==j: print j
else: res = res+k; print res; k--
newline | Approach | Idea | Best for |
|---|---|---|
| res + k stepping | res = res + k with k decreasing each step | Diagonal-fill sequence within each row |
| StringBuilder row | Build row without trailing spaces | Cleaner console output — Example 3 |
| Scanner input | sc.nextInt() for rows | User-chosen row count |
| Compact output | StringBuilder joins values with single spaces | No trailing space per row — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | int rows = 5; |
| Outer loop | for (i = 1; i <= rows; i++) |
| Row setup | k = rows - 1; res = i; |
| Inner loop | for (j = i; j < i + i; j++) |
| First value | if (i == j) print(j) |
| Next values | res = res + k; print(res); k--; |
| Row break | System.out.println(); after both halves |
| Program 54 contrast | Diamond diagonal uses fixed-width rows; this triangle grows i values per row |
How row start, step variable, inner bounds, and row breaks work together.
res = i
if (i == j) print(j)First number on row i is always i.
k = rows - 1
res = res + k
k--Each subsequent value adds a shrinking step size.
for (j = i; j < i + i; j++)Row i prints exactly i numbers.
trace i=3, rows=5Dry-run row 3: start 3, then +4→7, +3→10 → 3 7 10.
Reach for this pattern when teaching step variables, running totals, and growing row lengths.
Classic follow-up after diamond diagonal patterns — introduces step-based number generation.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible pattern size.
Compare with Program 54 (fixed-width diamond), then continue to Program 56 palindromic pyramid.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that locks in step variables, running totals, and O(n²) thinking.
Choose pattern size n and draw the full diagonal-fill triangle number pattern in the browser.
Three complete Java programs — fixed n = 5, Scanner input, and a star-diagonal variant. Click View Output to reveal sample console results.
Print five rows — each row grows by one number using res + k stepping.
rows = 5Hard-coded rows = 5 — each row uses res + k stepping with k = rows - 1.
public class DiagonalFillNumberTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
int k = rows - 1;
int res = i;
for (int j = i; j < i + i; j++) {
if (i == j) {
System.out.print(j + " ");
} else {
res = res + k;
System.out.print(res + " ");
k--;
}
}
System.out.println();
}
}
} Row 1 prints just 1. Row 3 starts at 3, adds 4 to get 7, adds 3 to get 10 — output 3 7 10.
Read rows with Scanner for flexible output size.
Same stepping logic; row count comes from user input.
import java.util.Scanner;
public class DiagonalFillNumberTriangleInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
int k = rows - 1;
int res = i;
for (int j = i; j < i + i; j++) {
if (i == j) System.out.print(j + " ");
else {
res = res + k;
System.out.print(res + " ");
k--;
}
}
System.out.println();
}
sc.close();
}
} Same stepping logic as Example 1; row count comes from Scanner input.
Build each row with StringBuilder — no trailing space.
Same logic; use StringBuilder to join values without a trailing space.
public class DiagonalFillNumberTriangleCompact {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
int k = rows - 1;
int res = i;
StringBuilder row = new StringBuilder();
for (int j = i; j < i + i; j++) {
if (row.length() > 0) row.append(" ");
if (i == j) row.append(j);
else {
res = res + k;
row.append(res);
k--;
}
}
System.out.println(row);
}
}
} Same stepping logic; StringBuilder produces clean rows without trailing spaces.
System.out is built in; use Scanner when reading input. Set rows (e.g. 5).
for (i = 1; i <= rows; i++) — one triangle row per iteration.
Each row: k = rows - 1, res = i — step size and running total reset.
j = i..i+i-1: print j when i==j, else res += k and k--; then println().
Total prints = n(n+1)/2 — O(n²) time, O(1) extra memory.
i = 3, rows = 5Trace row 3 to see how res + k stepping produces 3 7 10.
| Step | j | Action / row so far |
|---|---|---|
| Start | — | k=4, res=3 |
| First | 3 | i==j → print 3 → 3 |
| Second | 4 | res=3+4=7, k=3 → 3 7 |
| Third | 5 | res=7+3=10, k=2 → 3 7 10 |
Final row 3 output: 3 7 10. Then println() moves to row 4.
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 rows — see Example 2.
k starts at rows-1 and decreases — controls how far each step jumps.
Example: trace row 3 with rows=5 — 3, then +4→7, +3→10.
Practice println vs print for multi-line vs single-line output.
Example: use StringBuilder for clean rows — see Example 3.
Last value on row n is always the triangular number n(n+1)/2.
Example: rows=5 ends with 15 — total count of printed numbers.
Total prints n(n+1)/2 makes O(n²) concrete for beginners.
Example: count values for rows=5 → 1+2+3+4+5 = 15 prints.
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 step logic shows immediately — row values grow too fast or too slow.
Only loops and console output — no arrays or math libraries.
Change n, use Scanner, or print * on diagonals instead of numbers.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the res + k stepping first; then try Scanner input and the StringBuilder variant in Example 3.
Small habits that keep number-pattern code clean.
Use rows for height and i/j/k/res for loop variables.
ScannerAvoid crashes when the user types letters instead of a number.
Finish the inner loop for row i, then call println().
Set k = rows - 1 and res = i at the start of every outer iteration.
Trace rows = 5, i = 3 on paper — expect 3 7 10.
Pro Tip: if row values grow too fast, check whether k-- runs after each res + k step.
Mistakes that commonly break diagonal-fill triangle number patterns.
Each cell lands on its own line — you get a column, not an X-shaped row.
→ Use print inside both inner loops; println() only after they finish.
If k never decreases, every step adds the same offset — row values grow too fast.
→ Call k-- after each res = res + k update.
Omitting println() after both inner loops glues all rows onto one line.
→ Always call System.out.println() after both diagonal loops complete.
k must reset to rows - 1 at the start of each outer row, not once globally.
→ Place k = rows - 1 inside the outer loop, before the inner loop.
Letters or empty input throw InputMismatchException.
→ Use sc.hasNextInt() before sc.nextInt().
Using literal 5 in loop bounds instead of variable n breaks dynamic input.
→ Use one rows variable for outer loop and k initialization.
Check these inputs before calling the solution done.
Output is one line: 1 — bottom half loop does not run.
Loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Large values produce wide rows — fine for labs; use smaller n for quick demos.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
n = 3, 6, or 8i prints exactly i numbersk = rows instead of rows - 1n(n+1)/2 (e.g. 15 numbers for rows=5).print stays on the line; println advances — mix them carefully.rows > 0 for interactive programs; rows = 1 prints one number.if (i==j) print(j); else res = res + k; print(res); k--;.Quick Takeaway: outer i=1..rows, k=rows-1, res=i; inner j=i..i+i-1; step with res+k; then println().
| Program | Time | Extra space |
|---|---|---|
| Fixed rows = 5 (Example 1) | O(n²) | O(1) |
| Scanner input (Example 2) | O(n²) | O(1) |
| Compact rows (Example 3) | O(n²) | O(1) |
The diagonal-fill triangle combines a growing inner loop with decreasing step sizes — a natural step after fixed-width diamond patterns. Master the fixed-n version first, then try Scanner input and the star-diagonal variant in Example 3.
Practice the three examples above, then continue to Program 56 for the palindromic number pyramid pattern.
Reset k = rows - 1 and res = i at the start of every row — one println() per outer iteration.
i, res + k stepping, and inner bounds before codingk = rows - 1 and res = i at the start of each row; then println()rows ≥ 1 for interactive programsScanner return value before using rowsk = rows - 1 each rowk-- after each steprowsrows = 1 edge caseStart each row at i, then add shrinking steps via res + k.
Row i prints i numbers
Definitioni = 1..rows
Codek starts at rows-1
Logici values per row
O(n²)
AnalysisEach row starts with index i, then adds decreasing step sizes via res = res + k where k starts at rows - 1. Row i prints exactly i numbers.
Move on to the palindromic number pyramid pattern in the Java number-pattern series.
12 people found this page helpful