Java Diamond Diagonal Number Pattern

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

What Is This Pattern?

A diamond diagonal number pattern is Program 53’s X-shape for rows 1..n, then the same rows again from n−1 down to 1 — a full diamond of diagonals.

Remember
Rule: print diagonal-mirror rows for i = 1..n
      then again for i = n-1..1

1       1
 2     2 
  3   3  
   4 4   
    5    
   4 4   
  3   3  
 2     2 
1       1     ← n = 5 (9 rows)

Unlike Program 53 (top half only), here a second outer loop mirrors the pattern vertically without duplicating the center row.

How to Solve It

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

MethodIdeaBest for
Fixed nTwo outer loops; i==j / i==kLabs and demos
Scanner inputSame logic; read n at runtimeInteractive practice
Star diagonalsPrint * instead of the digitVisual diamond practice

Pseudocode

Pseudocode
function printRow(i, n):
    for j from 1 to n:
        if i == j: print j else print space
    for k from n-1 down to 1:
        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--)
Main / mirrorSame as Program 53: i==j, i==k
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 halves of a row finish

Build each row with print, then break once.

Live Preview

Change n and the full diamond diagonal pattern 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       1
 2     2 
  3   3  
   4 4   
    5    
   4 4   
  3   3  
 2     2 
1       1

Worked Walkthrough — n = 3

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

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

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

Java Programs

Three complete programs: fixed n = 5, Scanner input, and star diagonals. 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 DiamondDiagonalNumberPattern {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == j) System.out.print(j);
                else System.out.print(" ");
            }
            for (int k = n - 1; k >= 1; 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 = 1; j <= n; j++) {
                if (i == j) System.out.print(j);
                else System.out.print(" ");
            }
            for (int k = n - 1; k >= 1; k--) {
                if (i == k) System.out.print(k);
                else System.out.print(" ");
            }
            System.out.println();
        }
    }
}

How It Works

1. Top half. Same as Program 53: 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 middle row.

Example 2 — Scanner Input

Read n at runtime. Same two outer loops.

Java
import java.util.Scanner;

public class DiamondDiagonalNumberPatternInput {
    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 = 1; j <= n; j++) {
                System.out.print(i == j ? j : " ");
            }
            for (int k = n - 1; k >= 1; k--) {
                System.out.print(i == k ? k : " ");
            }
            System.out.println();
        }

        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j <= n; j++) {
                System.out.print(i == j ? j : " ");
            }
            for (int k = n - 1; k >= 1; k--) {
                System.out.print(i == k ? k : " ");
            }
            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 are identical to 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 — Star Diagonals

Same full diamond — print * instead of the diagonal digit.

Java
public class DiamondDiagonalNumberPatternStar {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.print(i == j ? "*" : " ");
            }
            for (int k = n - 1; k >= 1; k--) {
                System.out.print(i == k ? "*" : " ");
            }
            System.out.println();
        }

        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j <= n; j++) {
                System.out.print(i == j ? "*" : " ");
            }
            for (int k = n - 1; k >= 1; k--) {
                System.out.print(i == k ? "*" : " ");
            }
            System.out.println();
        }
    }
}

How It Works

1. Same positions. Still use i==j and i==k in both outer loops.

2. Different glyph. Print * instead of j/k — the diamond outline stays the same.

3. Same size. Still 2n − 1 rows and 2n − 1 characters per row.

Edge Cases & Pitfalls

Check these before calling the solution done.

Bottom from n

Doubled center

Starting the second loop at i = n reprints the middle row. Always start at n − 1.

k from n

Doubled column

Mirror half must start at k = n − 1 — same rule as Program 53.

Missing spaces

Collapsed X

Skipping else print(" ") packs digits together and destroys the diamond shape.

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
All three examplesO(n²)O(1)

2n − 1 rows × 2n − 1 characters per row → about 4n² prints, still O(n²).

Key Takeaways

  • Rule: Program 53 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 X top-down, then mirror it upward without repeating the center.

Frequently Asked Questions

Program 53 prints only n rows (the top half). Program 54 adds a second outer loop from n-1 down to 1 to mirror the output vertically.
Starting at n would duplicate the middle row (the widest point). n-1 down to 1 mirrors without repeating the center.
For size n, the diamond has 2n-1 rows: n for the top half and n-1 for the bottom half.
At i=n, both diagonals meet at the same position, so only one digit appears on the center row.
Yes. Use a variable n — both outer loops reuse the same diagonal row logic.
Replace the printed digit with * when i==j or i==k in both outer loops — see Example 3.
O(n²) for (2n-1) rows. Each row checks O(n) cells in two inner loops.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

Program 54 prints the diagonal mirror pattern (Program 53) for rows 1..n, then repeats the same logic for rows n-1..1 — forming a full diamond with numbers on both diagonals.

Next: Diagonal-Fill Number Triangle

Continue with the diagonal-fill number triangle pattern in the Java number-pattern series.

Program 55 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