Left Edge Loop
j = n..1
Scan j from n down to 1; print i when i == j, else a space.

The hollow number pyramid prints each row number at the left and right edges only — spaces fill the middle. For n = 5: 1, 2 2, 3 3, and so on. Each row uses a left loop j = n..1 and a right loop k = 2..n with i == j / i == k conditions. This tutorial covers edge-printing logic, live preview, worked Java examples, edge cases, and O(n²) complexity.
j = n..1
Scan j from n down to 1; print i when i == j, else a space.
k = 2..n
Scan k from 2 to n; print i when i == k, else a space.
spaces only
Every position that is not an edge prints a space — that creates the hollow look.
2n-1 cols
Row i spans 2n-1 columns — row number at left and right edges (row 1 overlaps).
3–12 rows
Pick row count and draw the hollow pyramid instantly in the browser.
Complexity
Each row scans O(n) positions — total work grows as n².
A hollow number pyramid prints the row number at both pyramid edges and spaces everywhere else. Row 2 reads 2 2; row 3 reads 3 3 when aligned.
In Java: outer for (i = 1; i <= n; i++), left loop j = n..1 with i == j, right loop k = 2..n with i == k, then println().
Given n = 5, print five hollow rows — widest row has 5 at both ends.
// n = 5 (conceptual output)
// 1
// 2 2
// 3 3
// 4 4
// 5 5 | Item | Type | Description |
|---|---|---|
n | int | Pyramid height — number of rows (typically ≥ 1). |
i, j, k | int | Row i; left index j; right index k. |
| Printed output | text | 2n-1 columns per row; row number at left and right edges only. |
for i from 1 to n:
for j from n down to 1: print i if i==j else space
for k from 2 to n: print i if i==k else space
newline | Approach | Idea | Best for |
|---|---|---|
| Two-loop row | Left edge loop + right edge loop | Hollow edge-only rows |
| StringBuilder row | Build row without trailing spaces | Cleaner console output — Example 3 |
| Scanner input | sc.nextInt() for n | User-chosen pyramid height |
| Compact output | StringBuilder joins values with single spaces | No trailing space per row — Example 3 |
| Goal | Pattern |
|---|---|
| Set n | int n = 5; |
| Outer loop | for (i = 1; i <= n; i++) |
| Left loop | for (j = n; j >= 1; j--) print j if i==j else space |
| Right loop | for (k = 2; k <= n; k++) print k if i==k else space |
| Row break | System.out.println(); after both edge loops |
| Program 56 contrast | Palindromic pyramid fills every row; this pattern prints edges only with spaces inside |
How left edge loop, right edge loop, and row breaks work together.
for (j = n; j >= 1; j--)
print(i==j ? j : " ")Right-aligns the left pyramid edge.
for (k = 2; k <= n; k++)
print(i==k ? k : " ")Expands the right pyramid edge outward.
trace i=3, n=5Dry-run row 3: left prints 3, right prints 3, middle is spaces.
Reach for this pattern when teaching edge conditions, hollow shapes, and nested loops.
Classic follow-up after palindromic pyramids — introduces hollow edge-printing.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible pattern size.
Compare with Program 56 (palindromic pyramid), then continue to Program 58 hollow diamond.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that locks in edge conditions, hollow rows, and O(n²) thinking.
Choose pattern size n and draw the full hollow number pyramid pattern in the browser.
Three complete Java programs — fixed n = 5, Scanner input, and a compact StringBuilder variant. Click View Output to reveal sample console results.
Print five hollow rows — widest row shows 5 at both edges.
n = 5Hard-coded n = 5 — left loop j = n..1, right loop k = 2..n, edge conditions i==j / i==k.
public class HollowNumberPyramid {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = n; j >= 1; j--) {
if (i == j) System.out.print(j);
else System.out.print(" ");
}
for (int k = 2; k <= n; k++) {
if (i == k) System.out.print(k);
else System.out.print(" ");
}
System.out.println();
}
}
} Row 1 prints one 1 where edges overlap. Row 3 prints 3 at the left edge and 3 at the right — spaces fill the middle.
Read n with Scanner for flexible output size.
Same hollow edge logic; pyramid height comes from user input.
import java.util.Scanner;
public class HollowNumberPyramidInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = n; j >= 1; j--) {
if (i == j) System.out.print(j);
else System.out.print(" ");
}
for (int k = 2; k <= n; k++) {
if (i == k) System.out.print(k);
else System.out.print(" ");
}
System.out.println();
}
sc.close();
}
} Same hollow edge logic as Example 1; height comes from Scanner input.
Build each row with StringBuilder — same edge logic, cleaner assembly.
Same edge logic; StringBuilder assembles left and right halves in one buffer.
public class HollowNumberPyramidCompact {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
StringBuilder row = new StringBuilder();
for (int j = n; j >= 1; j--) row.append(i == j ? j : " ");
for (int k = 2; k <= n; k++) row.append(i == k ? k : " ");
System.out.println(row);
}
}
} Same edge logic; StringBuilder avoids repeated System.out.print calls per character.
Set n (e.g. 5). Outer loop for (i = 1; i <= n; i++) builds one hollow row per iteration.
for (j = n; j >= 1; j--) prints i when i==j, else a space.
for (k = 2; k <= n; k++) prints i when i==k, else a space.
System.out.println() after both edge loops finish — starts the next row.
Each row scans 2n-1 positions (e.g. 9 columns when n=5) — O(n²) time, O(1) extra memory.
i = 3, n = 5Trace row 3 to see left and right edge loops form 3 3.
| Phase | Loop | Row so far |
|---|---|---|
| Left edge | j=5..1, i==3 at j=3 | 3 |
| Right edge | k=2..5, i==3 at k=3 | 3 3 |
Final row 3: 3 3. 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.
Split each row into left half (j=n..1) and right half (k=2..n) for edge control.
Example: trace row 3 with n=5 — left prints 3 at column 3, right at column 5.
Practice println vs print for multi-line vs single-line output.
Example: use StringBuilder for clean rows — see Example 3.
Only edge positions print digits — interior stays empty for the hollow effect.
Example: row 4 reads 4 4 — two fours at the edges.
Each row scans O(n) positions — total work grows as n².
Example: count columns on row 5 → 2×5-1 = 9 character positions per row.
Pair the pattern with Scanner and positive-row checks.
Example: reject n <= 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.
Missing right edge loop shows immediately — only left digits appear.
Only loops and console output — no arrays or math libraries.
Change n, use Scanner, mirror into a diamond (Program 58), or build rows with StringBuilder.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the two-loop row (left edge, right edge) first; then try Scanner input and the StringBuilder variant in Example 3.
Small habits that keep number-pattern code clean.
Use n for height and i/j/k for loop variables.
ScannerAvoid crashes when the user types letters instead of a number.
Finish the inner loop for row i, then call println().
Starting at k = 1 duplicates the center on row 1 — use k = 2 to skip overlap.
Trace n = 5, i = 3 on paper — expect 3 3 with spaces between edges.
Pro Tip: if row 1 shows two digits, check whether the right loop starts at k = 2.
Mistakes that commonly break hollow number pyramid patterns.
Each number lands on its own line — you get a vertical stack, not a centered pyramid row.
→ Use print inside left and right edge loops; println() only after they finish.
Without the k = 2..n loop, only the left edge prints — no right-side digit.
→ Add the right loop: for (k = 2; k <= n; k++) with i == k condition.
Omitting println() after both inner loops glues all rows onto one line.
→ Always call System.out.println() after left and right edge loops complete.
Starting k = 1 duplicates the center on row 1 — use k = 2 instead.
→ Start the right half at k = 2 to skip the overlapping center column.
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 n variable for outer loop and both inner loop bounds.
Check these inputs before calling the solution done.
Output is one line: 1 — right edge overlaps left on the single column.
Loop never runs — print nothing or show a message.
n < 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 n 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 shows i at both edges2n-1 columns; only two (or one on row 1) print digits.print stays on the line; println advances — mix them carefully.n > 0 for interactive programs; n = 1 prints one digit where edges overlap.j=n..1, then right loop k=2..n, edge conditions only.Quick Takeaway: outer i=1..n; left loop j=n..1; right loop k=2..n; then println().
| Program | Time | Extra space |
|---|---|---|
| Fixed n = 5 (Example 1) | O(n²) | O(1) |
| Scanner input (Example 2) | O(n²) | O(1) |
| Compact rows (Example 3) | O(n²) | O(1) |
The hollow number pyramid combines left/right edge loops with space padding — a natural step after the palindromic pyramid in Program 56. Master the fixed-n version first, then try Scanner input and the compact StringBuilder variant in Example 3.
Practice the three examples above, then continue to Program 58 for the hollow number diamond pattern.
Print edge digits only — spaces everywhere else — one println() per outer iteration.
i, left loop, and right loop before codingprintln()n ≥ 1 for interactive programsScanner return value before using nk = 1 (duplicates center on row 1)nn = 1 edge casePrint row number at left and right edges; spaces fill the hollow middle.
Row i has 2 edge digits (1 when i=1)
Definitioni = 1..n
CodeLeft j=n..1, right k=2..n
Logic2n-1 cols/row
O(n²)
AnalysisEach row prints the row number at the left and right edges only — everything between is spaces. Row 1 overlaps at one position; row 5 reads 5 5.
Move on to the hollow number diamond pattern in the Java number-pattern series.
12 people found this page helpful