Concentric-Style Number Square in Java

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a symmetric number pattern in Java where values decrease toward the center and then increase again.

For k = 5, the last line becomes 5 4 3 2 1 2 3 4 5.

⭐ Pattern Output

For k = 5, the pattern looks like this:

Output
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
1

Complete Java Program

Print the left half using j=k..1, then mirror the right half using j=2..k. Each cell prints the bigger value between the row index and the current column value.

Java
public class Main {
    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

Set the maximum value

k = 5 determines how many rows are printed and the outermost value.

Setup
2

Outer loop selects the row value

i goes from k down to 1, so each next row gets closer to the center value.

Rows
3

Left half prints k..1

The loop j = k..1 chooses max(i, j) (implemented with if (j > i)).

Left
4

Right half mirrors 2..k

The second loop prints j = 2..k with the same max(i, j) rule, creating symmetry.

Mirror
=

Symmetric max-pattern rows

Because every cell prints max(i, j), the smallest number appears only in the center of the last row.

2

Variation — User Input Version

Let the user pick k at runtime using Scanner:

Java
import java.util.Scanner;

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

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

💡 Tips for Enhancement

Try These

  • Print a full concentric square (2k-1 by 2k-1) by generating both top and bottom halves
  • Replace spaces with tabs for aligned columns in some consoles
  • Print without trailing spaces by building each row using StringBuilder
  • Try the inverse (use min(i, j)) to get different shapes
  • Use the same idea to build distance-based patterns

Avoid

  • Hard-coding 5 everywhere instead of using k
  • Mixing the mirror bounds (ensure the right half starts at 2)
  • Forgetting System.out.println() after each row
  • Closing System.in with Scanner if you need input later in the same JVM run

Key Takeaways

1

Each row prints values based on max(i, j).

2

The left half prints k..1; the right half mirrors 2..k.

3

The smallest value appears at the center of the final row.

4

This is a reusable pattern technique: grid + rule.

❓ Frequently Asked Questions

When i=1 (last row), the rule prints j for all j>1, but prints 1 at the center, so the row becomes 5 4 3 2 1 2 3 4 5.
It’s a concentric-style output for the top half (k rows). A full concentric square is typically (2k-1)×(2k-1) with both top and bottom halves.
Yes. Build the row using StringBuilder and trim the final space before printing.
O(k²) because you print \(k\) rows with \(2k-1\) values each.

Explore More Java Number Patterns!

Concentric-style patterns are excellent practice for conditional logic inside nested loops.

All Number Patterns →
Did you know?

You can often describe pattern cells using a simple math rule like max(), min(), or distance from the edge. Once you find the rule, the code becomes much simpler.

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