Concentric-Style Number Square in Java

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:
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 5Complete 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.
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
Set the maximum value
k = 5 determines how many rows are printed and the outermost value.
Outer loop selects the row value
i goes from k down to 1, so each next row gets closer to the center value.
Left half prints k..1
The loop j = k..1 chooses max(i, j) (implemented with if (j > i)).
Right half mirrors 2..k
The second loop prints j = 2..k with the same max(i, j) rule, creating symmetry.
Symmetric max-pattern rows
Because every cell prints max(i, j), the smallest number appears only in the center of the last row.
Variation — User Input Version
Let the user pick k at runtime using Scanner:
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.inwithScannerif you need input later in the same JVM run
Key Takeaways
Each row prints values based on max(i, j).
The left half prints k..1; the right half mirrors 2..k.
The smallest value appears at the center of the final row.
This is a reusable pattern technique: grid + rule.
❓ Frequently Asked Questions
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.(2k-1)×(2k-1) with both top and bottom halves.StringBuilder and trim the final space before printing.Explore More Java Number Patterns!
Concentric-style patterns are excellent practice for conditional logic inside nested loops.
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.
12 people found this page helpful
