Concentric Number Diamond in Java

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:
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 5Complete Java Program
This version prints the top half (k..1) and then prints the bottom half (2..k) to mirror the output.
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
Set the maximum value
k = 5 is the outermost number and controls the size.
First part prints k down to 1
The outer loop runs i = k..1, shrinking toward the center row.
Each cell prints max(i, j)
The condition j > i prints j; otherwise prints i. That is exactly max(i, j).
Second part mirrors back to k
A second outer loop runs i = 2..k to print the bottom half and complete the symmetry.
Full concentric-style diamond
Top + mirrored bottom gives a complete symmetric output with a single center.
Variation — User Input Version
Let the user choose k 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();
}
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
kto generate larger symmetric outputs
Avoid
- Hard-coding 5 everywhere; use
kas 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.inwithScannerif you need input later in the same JVM run
Key Takeaways
Generate the top half with i = k..1.
Mirror it back with i = 2..k (to avoid duplicating the center row).
Each cell prints max(i, j) over a symmetric column sequence.
This “two halves + mirror” technique is reusable for many patterns.
❓ Frequently Asked Questions
i=5..1) and the bottom half prints 4 rows (i=2..5). Total rows = 5 + 4 = 2k-1 = 9.k..1) and a mirrored right half (2..k), so total columns are k + (k-1) = 2k-1.(2k-1)×(2k-1) and fill it using a mathematical rule, then print it.Explore More Java Number Patterns!
Symmetric “ring” patterns are great practice for nested loops and conditions.
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.
12 people found this page helpful
