Top Half
i = 1..n
First outer loop prints the hollow pyramid — same edge logic as Program 57.

The hollow number diamond prints Program 57’s hollow pyramid for rows 1..n, then mirrors rows n-1..1. For n = 5 you get nine rows forming a symmetric hollow diamond. Same edge logic on every row — two outer loops drive top and bottom halves. This tutorial covers mirroring, live preview, worked Java examples, edge cases, and O(n²) complexity.
i = 1..n
First outer loop prints the hollow pyramid — same edge logic as Program 57.
i==j, i==k
Left loop j=n..1 and right loop k=2..n print row number at edges only.
i = n-1..1
Second outer loop mirrors rows back down — start at n-1 to skip duplicate middle.
2n-1 cols
Row i spans 2n-1 columns — row number at left and right edges (row 1 overlaps).
3–12 rows
Pick diamond size and draw the hollow diamond instantly in the browser.
Complexity
Each row scans O(n) positions — total work grows as n².
A hollow number diamond is Program 57’s hollow pyramid plus a mirrored bottom half. Row 2 reads 2 2; the widest row shows 5 5; then rows shrink back to 1.
In Java: top loop for (i = 1; i <= n; i++), bottom loop for (i = n-1; i >= 1; i--) — each row uses left/right edge loops, then println().
Given n = 5, print nine hollow rows — pyramid up, then mirror down.
// n = 5 (conceptual output — 2n-1 = 9 rows)
// 1
// 2 2
// 3 3
// 4 4
// 5 5
// 4 4
// 3 3
// 2 2
// 1 | Item | Type | Description |
|---|---|---|
n | int | Half-height — diamond has 2n-1 rows total (typically n ≥ 1). |
i, j, k | int | Row i; left index j; right index k. |
| Printed output | text | 2n-1 columns per row; 2n-1 rows total; edges only. |
for i from 1 to n: print hollow row i
for i from n-1 down to 1: print hollow row i
(each row: left j=n..1, right k=2..n, edge conditions) | Approach | Idea | Best for |
|---|---|---|
| Two outer loops | Top half i=1..n + bottom half i=n-1..1 | Complete hollow diamond |
| printRow helper | Extract row printing into a method | Less duplicated code — Example 3 |
| Scanner input | sc.nextInt() for n | User-chosen pyramid height |
| Compact output | StringBuilder joins values with single spaces | Reusable row builder — 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 |
| Bottom half | for (i = n-1; i >= 1; i--) — same row logic, mirror down |
| Skip duplicate middle | System.out.println(); after both edge loops per row |
| Program 57 contrast | Hollow pyramid is top half only; this diamond mirrors the bottom from n-1 down to 1 |
How top half, bottom half, and shared row logic work together.
for (i = 1; i <= n; i++)
printRow(i, n)Hollow pyramid rows 1 through n.
for (i = n-1; i >= 1; i--)
printRow(i, n)Mirror back — skip row n to avoid duplicate.
j=n..1, k=2..n
i==j or i==k → digitSame edge logic on every row — Program 57 core.
trace n=5, i=3Row 3 appears twice — once going up, once mirrored down.
Reach for this pattern when teaching mirroring, diamonds, and nested loops.
Classic follow-up after hollow pyramids — introduces top-half + mirrored bottom-half.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible pattern size.
Compare with Program 57 (hollow pyramid), then continue to Program 59 hollow square border.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that locks in mirroring, diamond symmetry, and O(n²) thinking.
Choose pattern size n and draw the full hollow number diamond 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 nine hollow rows — widest row shows 5 at both edges.
n = 5Hard-coded n = 5 — top half i=1..n, bottom half i=n-1..1 — same edge loops per row.
public class HollowNumberDiamond {
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();
}
for (int i = n - 1; i >= 1; 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();
}
}
} First loop builds the hollow pyramid (Program 57). Second loop mirrors from n-1 down — row 5 appears once at the widest point.
Read n with Scanner for flexible diamond size.
Same top + bottom diamond logic; half-height comes from user input.
import java.util.Scanner;
public class HollowNumberDiamondInput {
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();
}
for (int i = n - 1; i >= 1; 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 top + bottom diamond logic as Example 1; half-height comes from Scanner input.
Extract row printing into a printRow helper — less duplicated code.
Extract printRow to avoid duplicating edge loops in top and bottom halves.
public class HollowNumberDiamondCompact {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) printRow(i, n);
for (int i = n - 1; i >= 1; i--) printRow(i, n);
}
static void printRow(int i, int n) {
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);
}
} One printRow method serves both halves — cleaner and easier to maintain.
Outer loop for (i = 1; i <= n; i++) prints the hollow pyramid — rows 1 through n.
Each row: left loop j=n..1, right loop k=2..n — print digit at edges, space elsewhere.
Second outer loop for (i = n-1; i >= 1; i--) mirrors rows back down.
Start bottom half at n-1, not n — prevents printing the widest row twice.
Total 2n-1 rows (e.g. 9 when n=5) — O(n²) time, O(1) extra memory.
i = 3, n = 5Trace row 3 to see top half reaches row 3, then bottom half mirrors it back.
| Phase | Loop | Row so far |
|---|---|---|
| Top half | i=1..5, row 3 at i=3 | pyramid grows to widest row |
| Bottom half | i=4..1, row 3 again at i=3 | mirror shrinks back to 1 |
Row 3 appears in both halves. 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.
Two outer loops share the same row logic — classic mirror pattern.
Example: trace n=5 — 9 total rows, middle row 5 appears once.
Practice println vs print for multi-line vs single-line output.
Example: use StringBuilder for clean rows — see Example 3.
Top pyramid plus mirrored bottom — symmetric hollow diamond.
Example: row 4 appears twice — once ascending, once descending.
Each row scans O(n) positions — total work grows as n².
Example: count total rows → 2×5-1 = 9 rows for n=5.
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.
Starting bottom loop at n duplicates the widest row immediately.
Only loops and console output — no arrays or math libraries.
Change n, use Scanner, extract printRow, or continue to Program 59.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the top + bottom halves first; then try Scanner input and the printRow helper 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 on paper — expect 2n-1 = 9 rows with row 3 appearing twice.
Pro Tip: if the widest row prints twice, check whether the bottom loop starts at n-1.
Mistakes that commonly break hollow number diamond patterns.
Each number lands on its own line — you get a vertical stack, not a hollow diamond row.
→ Use print inside left and right edge loops; println() only after they finish.
Starting bottom loop at i = n prints the widest row twice.
→ Use for (i = n-1; i >= 1; i--) so the middle row is not duplicated.
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 at k = 1 duplicates the center digit on row 1 — use k = 2 instead.
→ Right half starts 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 82n-1 rows2n-1 rows; widest row appears once when bottom starts at n-1.print stays on the line; println advances — mix them carefully.n > 0 for interactive programs; n = 1 prints one digit where edges overlap.Quick Takeaway: top i=1..n, bottom i=n-1..1; each row uses left/right edge loops; 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 diamond combines Program 57’s pyramid with a mirrored bottom half — a natural step after the hollow pyramid in Program 57. 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 59 for the hollow square border pattern.
Mirror from n-1 — never duplicate the middle row — one println() per outer iteration.
n ≥ 1 for interactive programsScanner return value before using nnn = 1 edge caseTop half builds pyramid; bottom half mirrors from n-1 down to 1.
Diamond has 2n-1 rows total
Definitioni = 1..n
CodeTop 1..n, bottom n-1..1
Logic2n-1 cols/row
O(n²)
AnalysisProgram 57’s hollow pyramid printed once, then mirrored from n-1 down to 1 — that gives a hollow diamond with 2n-1 rows.
Move on to the hollow square border pattern in the Java number-pattern series.
12 people found this page helpful