Concentric Number Diamond in Java

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

What You’ll Learn

How to print a concentric-style number pattern in Java that shrinks toward the center and then expands again.

For k = 5, the center row becomes 5 4 3 2 1 2 3 4 5 and the whole output is symmetric.

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

Complete Java Program

This version prints the top half (k..1) and then prints the bottom half (2..k) to mirror the output.

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

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

        // Second Part
        for (int i = 2; i <= k; 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 is the outermost number and controls the size.

Setup
2

First part prints k down to 1

The outer loop runs i = k..1, shrinking toward the center row.

Top
3

Each cell prints max(i, j)

The condition j > i prints j; otherwise prints i. That is exactly max(i, j).

Rule
4

Second part mirrors back to k

A second outer loop runs i = 2..k to print the bottom half and complete the symmetry.

Mirror
=

Full concentric-style diamond

Top + mirrored bottom gives a complete symmetric output with a single center.

2

Variation — User Input Version

Let the user choose k 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();
        }

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

💡 Tips for Enhancement

Try These

  • Print without trailing spaces by building each row with StringBuilder
  • Change the rule to min(i, j) to see a very different pattern
  • Make a full concentric square by using distance-from-edge logic
  • Use tabs instead of spaces to align columns in some terminals
  • Increase k to generate larger symmetric outputs

Avoid

  • Hard-coding 5 everywhere; use k as the single control variable
  • Starting the mirrored half at 1 (it would duplicate the center row)
  • Mixing loop bounds between left and right halves (keep them symmetric)
  • Closing System.in with Scanner if you need input later in the same JVM run

Key Takeaways

1

Generate the top half with i = k..1.

2

Mirror it back with i = 2..k (to avoid duplicating the center row).

3

Each cell prints max(i, j) over a symmetric column sequence.

4

This “two halves + mirror” technique is reusable for many patterns.

❓ Frequently Asked Questions

The top half prints 5 rows (i=5..1) and the bottom half prints 4 rows (i=2..5). Total rows = 5 + 4 = 2k-1 = 9.
Each row prints a left half (k..1) and a mirrored right half (2..k), so total columns are k + (k-1) = 2k-1.
Yes. You can build a 2D array of size (2k-1)×(2k-1) and fill it using a mathematical rule, then print it.
O(k²) because the output size is \((2k-1)^2\), which is proportional to \(k^2\).

Explore More Java Number Patterns!

Symmetric “ring” patterns are great practice for nested loops and conditions.

All Number Patterns →
Did you know?

Concentric patterns often have a clean math description based on distance to the nearest edge. Once you find the rule, you can generate the whole grid without special-casing rows.

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