Java Hollow Number Diamond Pattern

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

Reuse the same edge-row logic twice: once ascending, once descending from n−1.

MethodIdeaBest for
Fixed nTwo outer loops; same edge logicLabs and demos
Scanner inputSame logic; read n at runtimeInteractive practice
printRow helperShared method for both halvesLess 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

GoalPattern
Set sizeint n = 5;
Top halffor (int i = 1; i <= n; i++)
Bottom halffor (int i = n - 1; i >= 1; i--)
Left / rightSame as Program 57: i==j, i==k (k from 2)
Total rows2n - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit or space in both halves
System.out.printlnEnds the lineAfter both edge loops of a row finish

Build each row with print, then break once.

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 result n = 5 · rows = 9
    1    
   2 2   
  3   3  
 4     4 
5       5
 4     4 
  3   3  
   2 2   
    1    

Worked Walkthrough — n = 3

Trace the five rows: top 1..3, then bottom 2..1.

HalfiPrinted row
Top11
Top22 2
Top (center)33 3
Bottom22 2
Bottom11

The bottom loop starts at n−1 so the widest row (i = n) appears only once.

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();
        }
    }
}

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();
    }
}

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);
    }
}

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.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–2O(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²).

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.

Next: Hollow Square Border Number Pattern

Continue with the hollow square border number pattern in the Java number-pattern series.

Program 59 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful