Java Concentric Number Square Pattern

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

What Is This Pattern?

A concentric-style number square prints max(i, j) on each cell — outer border stays at k, and values decrease toward the center.

Remember
Rule: for i from k down to 1,
      left j = k..1 then 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     ← k = 5

Unlike Program 45 (star cross on zeros), here every cell is a number from the max(i, j) rule.

How to Solve It

Walk i from k down to 1; on each row print the left half j = k..1, then mirror j = 2..k.

MethodIdeaBest for
If / ternaryPrint j when j > i, else iLearning, interviews, exams
Math.maxSame shape with Math.max(i, j)Cleaner code once the rule is clear

Pseudocode

Pseudocode
for i from k down to 1:
    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
Walk rowsfor (int i = k; i >= 1; i--)
Left halffor (int j = k; j >= 1; j--)
Right halffor (int j = 2; j <= k; j++)
Cell valuej > i ? j : i or Math.max(i, j)
End the rowSystem.out.println();

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 finish

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

Live Preview

Change k and the concentric square updates instantly.

Whole numbers from 2 to 7. Tap a chip or type a value — the preview redraws as you go.

Live result k = 5 · 5 rows · 9 cols
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 

Worked Walkthrough — k = 5, row i = 3

Trace left half j = 5..1 then right half j = 2..5 for one middle row.

Halfjmax(3, j)
Left5, 4, 3, 2, 15 4 3 3 3
Right2, 3, 4, 53 3 4 5
Full row5 4 3 3 3 3 3 4 5

Each row has 2k − 1 values — for k = 5 that is 9 numbers.

Java Programs

Three complete programs: fixed k = 5, Scanner input, and a Math.max compact form. Use View Output to reveal sample results.

Example 1 — Fixed k = 5

Hard-coded size — left half, right half, and an if/else for max(i, j).

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

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

How It Works

1. Outer loop. i runs from k down to 1 — largest ring first.

2. Left half. j counts down from k to 1 and prints max(i, j).

3. Right half. j counts up from 2 to k — mirrors the left side without repeating the center.

When i = 5: every cell is 5. When i = 1: the last row becomes 5 4 3 2 1 2 3 4 5.

Example 2 — k Input

Read k at runtime. Same two-half core with a ternary.

Java
import java.util.Scanner;

public class ConcentricNumberSquareInput {
    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();
        }
        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; the ternary replaces the if/else.

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 — Math.max Compact

Same shape — Math.max(i, j) replaces the if/else in both halves.

Java
public class ConcentricNumberSquareCompact {
    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(Math.max(i, j) + " ");
            for (int j = 2; j <= k; j++)
                System.out.print(Math.max(i, j) + " ");
            System.out.println();
        }
    }
}

How It Works

1. Same structure. Left and right half-loops are unchanged.

2. Clearer rule. Math.max(i, j) is the same as j > i ? j : i.

3. Next step tip. Program 47 often mirrors rows below the center for a full concentric square.

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.

j from 1

Double center

Starting the right half at j = 1 reprints the center column and breaks the symmetry.

min instead

Different shape

Using Math.min(i, j) flips the rings — useful to explore, but not this pattern.

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 right half 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 square (Examples 1–3)O(k²)O(1)

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

Key Takeaways

  • Rule: each cell prints max(i, j) — outer rings stay large.
  • Two halves: left j = k..1, right j = 2..k to avoid a double center.
  • Break the row: call println only after both half-loops.
  • Complexity: O(k²) time; O(1) extra space.

One line: for each i from k down to 1, print max(i, j) on the left half then the right half, then println().

Frequently Asked Questions

Because k=5 is the maximum. On any row i, column values near the edges use j when j>i, so the outer columns stay at k.
For row i and column j, the program prints j when j>i; otherwise i. That is equivalent to max(i, j).
When i=1, every j>1 prints j, but j=1 prints 1 at the center — creating the full symmetric sequence.
Yes. Replace k=5 with any positive integer. You get k rows and 2k-1 values per row.
This tutorial prints the top k rows (concentric-style). A full square often mirrors rows below the center too — see Program 47.
Yes. System.out.print(Math.max(i, j) + " ") works — see Example 3.
O(k²) because k rows each print about 2k-1 values.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

Each cell prints max(i, j) — the left half loops j = k..1, the right half mirrors with j = 2..k. The smallest value appears at the center of the last row.

Next: Concentric Number Diamond

Move on to the concentric number diamond in the Java number-pattern series.

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