A hollow number diamond is Program 57’s hollow pyramid for rows 1..n, then the same rows again from n−1 down to 1 — a full diamond of edge digits.
Remember
Rule: print hollow-pyramid rows for i = 1..n
then again for i = n-1..1
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1 ← n = 5 (9 rows)
Unlike Program 57 (top half only), here a second outer loop mirrors the pattern vertically without duplicating the center row.
Approach
How to Solve It
Reuse the same edge-row logic twice: once ascending, once descending from n−1.
Method
Idea
Best for
Fixed n
Two outer loops; same edge logic
Labs and demos
Scanner input
Same logic; read n at runtime
Interactive practice
printRow helper
Shared method for both halves
Less duplicated code
Pseudocode
Pseudocode
function printRow(i, n):
for j from n down to 1:
if i == j: print j else print space
for k from 2 to n:
if i == k: print k else print space
new line
for i from 1 to n: printRow(i, n)
for i from n-1 down to 1: printRow(i, n)
Cheat sheet
Goal
Pattern
Set size
int n = 5;
Top half
for (int i = 1; i <= n; i++)
Bottom half
for (int i = n - 1; i >= 1; i--)
Left / right
Same as Program 57: i==j, i==k (k from 2)
Total rows
2n - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit or space in both halves
System.out.println
Ends the line
After both edge loops of a row finish
Build each row with print, then break once.
Try it
Live Preview
Change n and the hollow number diamond updates instantly.
Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · rows = 9
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
Trace
Worked Walkthrough — n = 3
Trace the five rows: top 1..3, then bottom 2..1.
Half
i
Printed row
Top
1
1
Top
2
2 2
Top (center)
3
3 3
Bottom
2
2 2
Bottom
1
1
The bottom loop starts at n−1 so the widest row (i = n) appears only once.
Code
Java Programs
Three complete programs: fixed n = 5, Scanner input, and a shared printRow helper. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded size — top half 1..n, then bottom half n−1..1.
Java
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();
}
}
}
Output
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
How It Works
1. Top half. Same as Program 57: for each i = 1..n, print on i==j and i==k.
2. Bottom half. Repeat the identical row logic for i = n−1..1 — vertical mirror.
3. No duplicate center. Starting at n−1 skips reprinting the widest row.
Example 2 — Scanner Input
Read n at runtime. Same two outer loops.
Java
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();
if (n < 1) return;
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();
}
}
Output (when user enters 5)
Enter n: 5
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
How It Works
1. Prompt and guard. Read n; exit early if it is less than 1.
2. Same diamond. Only the source of n changes — top then bottom outer loops match Example 1.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int n = sc.nextInt();
if (n < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — printRow Helper
Extract printRow so top and bottom halves share one edge-loop implementation.
Java
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);
}
}
Output
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
How It Works
1. Shared row.printRow holds the left/right edge logic once.
2. Two calls. Top loop and bottom loop both call the helper — no duplicated inner loops.
3. Same shape. Output matches Examples 1 and 2 character for character.
Edge Cases & Pitfalls
Check these before calling the solution done.
Bottom from n
Doubled center
Starting the second loop at i = n reprints the widest row. Always start at n − 1.
k from 1
Doubled column
Right half must start at k = 2 — same rule as Program 57.
Missing spaces
Solid diamond
Skipping else print(" ") packs digits together and destroys the hollow outline.
n = 1
Single digit
Output is just 1 — the bottom loop does not run.
n = 0
Empty output
Neither outer loop runs. Guard interactive input with n >= 1.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–2
O(n²)
O(1)
StringBuilder helper (Example 3)
O(n²)
O(n) per row
2n − 1 rows × 2n − 1 characters per row → about 4n² prints, still O(n²).
Remember
Key Takeaways
Rule: Program 57 rows for 1..n, then again for n−1..1.
No duplicate: bottom half starts at n − 1.
Size:2n − 1 rows and 2n − 1 characters each.
Complexity:O(n²) for size n.
One line: print the hollow pyramid top-down, then mirror it upward without repeating the center.
Frequently Asked Questions
Program 57 prints only the top half (hollow pyramid). Program 58 adds a mirrored bottom half to form a complete diamond.
Starting at n would duplicate the widest middle row. n-1 mirrors back without repeating row n.
For size n, the diamond has 2n-1 rows — n rows up, then n-1 rows down.
At i=1, the left edge prints 1 and the right loop starts at k=2, so only one digit appears.
Yes. Use variable n in both outer loops and all inner bounds.
Second outer loop: for (i = n-1; i >= 1; i--) — never start at n again.
O(n²) because you print 2n-1 rows and each row scans about 2n-1 positions.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
🤔
Did you know?
Program 57’s hollow pyramid printed once, then mirrored from n-1 down to 1 — that gives a hollow diamond with 2n-1 rows.