Java Concentric Number Diamond Pattern

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

What Is This Pattern?

A concentric number diamond prints the Program 46 top half (i = k..1), then mirrors it with i = 2..k — a full symmetric square of rings.

Remember
Rule: top i = k..1, then bottom i = 2..k
      each row: left j = k..1, right j = 2..k; print max(i, j)

5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5     ← k = 5

Unlike Program 46 (top half only), here a second outer loop completes the diamond.

How to Solve It

Print rows for i = k..1, then repeat the same row logic for i = 2..k.

MethodIdeaBest for
Two outer halvesTop k..1, bottom 2..k; each row uses max(i, j)Learning, interviews, exams
printRow helperOne method for both halves — same output, less duplicationCleaner code once the row rule is clear

Pseudocode

Pseudocode
for i from k down to 1:     // top
    print row for i
for i from 2 to k:          // bottom
    print row for i

print row for i:
    for j from k down to 1: print max(i, j)
    for j from 2 to k:      print max(i, j)
    print newline

Cheat sheet

GoalPattern
Top halffor (int i = k; i >= 1; i--)
Bottom halffor (int i = 2; i <= k; i++)
Left / rightj = k..1 then j = 2..k
Cell valuej > i ? j : i or Math.max(i, j)
Total size2k − 1 rows and columns

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach number (with a trailing space)
System.out.printlnEnds the current lineAfter both half-loops of a row finish

Print values without a newline, then end the row once.

Live Preview

Change k and the concentric diamond updates instantly.

Whole numbers from 2 to 5 (keeps the full diamond readable). Tap a chip or type a value — the preview redraws as you go.

Live result k = 5 · 9 × 9
5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5 

Worked Walkthrough — k = 5

Trace which i values print in each half and why the center row appears once.

Halfi valuesRows
Top5, 4, 3, 2, 15 rows (includes the center)
Bottom2, 3, 4, 54 rows (skips i = 1)
Center rowi = 15 4 3 2 1 2 3 4 5

Total rows = 5 + 4 = 9 = 2k − 1. Starting the bottom at 2 avoids a double center.

Java Programs

Three complete programs: fixed k = 5, Scanner input, and a printRow helper. Use View Output to reveal sample results.

Example 1 — Fixed k = 5

Hard-coded size — top half then mirrored bottom half.

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

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

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

How It Works

1. Top half. Same as Program 46 — i from k down to 1.

2. Bottom half. i from 2 to k reprints the rings growing outward again.

3. Cell rule. Each value is still max(i, j) on left and right halves.

When i = 1: center row 5 4 3 2 1 2 3 4 5. When i = 5 (bottom): every cell is 5 again.

Example 2 — k Input

Read k at runtime. Same two-half core.

Java
import java.util.Scanner;

public class ConcentricNumberDiamondInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter k (e.g., 5): ");
        int k = sc.nextInt();
        if (k < 1) return;

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

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

How It Works

1. Prompt and guard. Read k; exit early if it is less than 1.

2. Same core. Only the source of k changes from a literal to user input.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive integer.");
    return;
}
int k = sc.nextInt();
if (k < 1) {
    System.out.println("Enter a positive integer.");
    return;
}

Example 3 — printRow Helper

One printRow(i, k) method — both outer loops call it.

Java
public class ConcentricNumberDiamondHelper {
    static void printRow(int i, int k) {
        for (int j = k; j >= 1; j--)
            System.out.print(Math.max(i, j) + " ");
        for (int j = 2; j <= k; j++)
            System.out.print(Math.max(i, j) + " ");
        System.out.println();
    }

    public static void main(String[] args) {
        int k = 5;

        for (int i = k; i >= 1; i--)
            printRow(i, k);
        for (int i = 2; i <= k; i++)
            printRow(i, k);
    }
}

How It Works

1. Extract the row. printRow owns left half, right half, and println.

2. Short mains. Top and bottom loops only choose which i to pass.

3. Math.max. Same cell rule as the ternary — easier to read in a helper.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Column of numbers

If println is inside either half-loop, each value lands on its own line. Use print for values; println only after both halves.

Bottom from 1

Double center

Starting the bottom half at i = 1 reprints the center row and breaks the diamond.

Skip bottom

Looks like Program 46

Omitting the second outer loop prints only the top half.

No println

One long line

Forgetting println() after the row glues every number onto one endless line.

k = 1

Single cell

Output is just 1 — the bottom loop never runs.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
Concentric diamond (Examples 1–3)O(k²)O(1)

2k − 1 rows each print 2k − 1 values — still quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: each cell prints max(i, j) on left and right halves.
  • Two halves: top k..1, bottom 2..k — skip i = 1 on the way back.
  • DRY tip: extract printRow so both outer loops stay short.
  • Complexity: O(k²) time; O(1) extra space.

One line: print Program 46’s top half, then mirror with i = 2..k using the same max(i, j) row.

Frequently Asked Questions

Top half prints 5 rows (i=5..1) and bottom half prints 4 rows (i=2..5). Total = 2k-1 = 9.
Left half k..1 plus right half 2..k gives k + (k-1) = 2k-1 values per row.
Starting at 2 avoids duplicating the center row (i=1) that already printed in the top half.
Print j when j>i; otherwise i — equivalent to max(i, j).
Program 46 prints only the top k rows. This pattern adds a bottom mirror loop i=2..k for a full diamond.
Yes. Extract printRow(i, k) and call it from both outer loops — see Example 3.
O(k²) because 2k-1 rows each print about 2k-1 values.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

Print the top half with i = k..1, then mirror with i = 2..k. Each cell uses max(i, j) over left j = k..1 and right j = 2..k — total rows = 2k-1.

Next: Powers of 11 Pattern

Move on to the powers of 11 pattern in the Java number-pattern series.

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