Shape Rule
max(i,j)
Print j when j > i; otherwise i — same rule as Program 46, applied to both halves.

The concentric number diamond prints a full symmetric shape: top half i = k..1, bottom mirror i = 2..k, each cell from max(i, j). This tutorial covers both outer loops, live preview, algorithm steps, worked Java examples, edge cases, and complexity.
max(i,j)
Print j when j > i; otherwise i — same rule as Program 46, applied to both halves.
i = k..1
for (i = k; i >= 1; i--) shrinks toward the center row.
i = 2..k
for (i = 2; i <= k; i++) expands back out — skips i=1 to avoid duplicating center.
j = k..1, 2..k
Each row uses left j=k..1 and right j=2..k — 2k-1 values per row.
k = 3–8
Pick a value for k and draw the full concentric number diamond in the browser.
Complexity
Prints (2k-1)² values total — e.g. 81 for k=5; memory stays O(1).
A concentric number diamond pattern shrinks toward the center, then expands back symmetrically. With k = 5, you get 2k-1 = 9 rows; the center row is 5 4 3 2 1 2 3 4 5.
In Java you run a top-half loop (i = k..1), then a bottom mirror (i = 2..k). Each row prints max(i, j) over left and right column loops.
It extends Program 46 with one mirror loop — the classic two-halves diamond technique.
i = k..1 — shrink toward center.
i = 2..k — expand without repeating center.
Same cell rule in every row of both halves.
Follow Program 46 concentric square; continue to Program 48 powers of 11.
In short: top loop i=k..1, bottom loop i=2..k, each row prints max(i,j), then println().
Given k = 5, print a full diamond: top half i=k..1, bottom mirror i=2..k, each cell max(i,j).
// k = 5 (conceptual shape)
// 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 | Item | Type | Description |
|---|---|---|
k | int | Maximum value and row count (typically ≥ 1). |
| Printed output | text | 2k-1 rows rows, each with 2k-1 rows space-separated numbers. |
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 | Approach | Idea | Best for |
|---|---|---|
| Two outer loops + max rule | j>i ? j : i in both halves | Learning and interviews |
| User-input k | sc.nextInt(); | Flexible console programs |
| printRow helper | Math.max(i,j) in both loops | Cleaner production-style code |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = k; i >= 1; i--) |
| Top half | for (j = k; j >= 1; j--) |
| Bottom mirror | for (j = 2; j <= k; j++) |
| Value rule | j > i ? j : i or Math.max(i, j) |
| End the row | System.out.println(); |
| Program 46 contrast | Concentric square prints top k rows only; this diamond mirrors with i=2..k |
Same diamond row — how top and bottom outer loops reuse the max(i,j) rule.
i = k..1Left segment j=k..1 before the mirror
i = 2..kRight segment j=2..k completes each row
max(i,j)Print j when j>i; otherwise print i
trace i=3,j=2Dry-run cell (3,2): max(3,2) → prints 3
Reach for this pattern when teaching max(i,j) inside mirrored column loops.
Classic follow-up after concentric diamonds and series patterns.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 46 (concentric square), then continue to Program 48 (powers of 11).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(k²) thinking.
Choose a value for k and draw the concentric number diamond in the browser.
Three complete Java programs — fixed k, Scanner input, and a printRow helper variant. Click View Output to reveal sample console results.
Print nine rows for k=5 — top half plus mirrored bottom.
k = 5Two outer loops — top i=k..1 and bottom i=2..k — with the same inner max rule.
public class ConcentricNumberDiamond {
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();
}
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();
}
}
} The first loop prints rows 5 down to 1; the second loop prints rows 2 up to 5. Starting the bottom at i = 2 keeps the center row from printing twice.
Let the user choose k at runtime.
Read k with Scanner.nextInt() (check hasNextInt() in real apps).
import java.util.Scanner;
public class ConcentricNumberDiamondInput {
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();
}
} Same nested-loop core as Example 1; only the source of k changes. Non-numeric input throws InputMismatchException with nextInt() — check hasNextInt() for safer labs.
Extract row printing into a helper to avoid duplicating four inner loops.
One printRow(i, k) method — both outer loops call it.
public class ConcentricNumberDiamondHelper {
static void printRow(int i, int k) {
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();
}
public static void main(String[] args) {
int k = 5;
for (int i = k; i >= 1; i--) printRow(i, k);
for (int i = 2; i <= k; i++) printRow(i, k);
}
} printRow encapsulates the left/right inner loops and row break — both outer loops stay short and readable.
System.out is built in; use Scanner when reading input. Set k (fixed or from input).
for (i = k; i >= 1; i--) — shrinks toward the center row.
Left j=k..1 and right j=2..k print max(i,j), then println().
for (i = 2; i <= k; i++) — expands back out; skips center duplicate.
Total values printed ≈ (2k-1)² — O(k²) time, O(1) extra memory.
i = 2Trace the mirror boundary to see why the center row is not duplicated.
| Check | Result | Prints |
|---|---|---|
| Top half ends | i = 1 | center row printed once |
| Bottom half starts | i = 2 | avoids duplicate center |
| Total rows | 2k-1 | 9 when k=5 |
Full diamond: 2k-1 = 9 rows and 9 values per row when k=5.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use Math.max(i,j) instead of if-else — see Example 3.
Foundation for concentric layouts, symmetric grids, and distance-based rules.
Example: swap max for min(i,j) to explore a different shape.
Practice System.out.print vs row newline without complex math.
Example: put System.out.println() inside the inner loop by mistake.
Swap numbers for letters or stars once the max rule works.
Example: print row numbers with leading spaces for alignment.
Grid totals make O(k²) concrete for beginners.
Example: count values for k=5 → 9 rows × 9 values = 81 prints.
Pair the pattern with Scanner and positive-row checks.
Example: reject k <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Java courses.
Wrong mirror bounds (starting right half at 1) duplicate the center value.
Only loops and console output — no arrays or math libraries.
Change k, swap max for min, or extract printRow for cleaner code.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the top and bottom mirror loops first; then try the printRow helper in Example 3.
Small habits that keep number-pattern code clean.
Use k for the outer bound and keep i/j for row/column — or rename to row/col.
ScannerAvoid crashes when the user types letters instead of a number.
Only call System.out.println() after the inner loop finishes the row.
Compact: System.out.print(Math.max(i, j) + " "); in both inner loops.
Trace k = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of numbers per line, you almost certainly put System.out.println() inside the inner loop.
Mistakes that commonly break concentric number diamond patterns.
Each number lands on its own line — you get a column, not a symmetric row.
→ Use System.out.print for each value; System.out.println() only after both inner loops finish.
Starting the mirror loop at i = 1 prints the center row twice in the full diamond.
→ Keep for (i = 2; i <= k; i++) for the bottom half.
Starting the right inner loop at j = 1 prints the center value twice on every row.
→ Keep for (j = 2; j <= k; j++) so the center appears once per row.
Omitting System.out.println() glues every row onto one endless line.
→ Always end the row after both inner loops complete.
Letters or empty input throw InputMismatchException.
→ Prefer Scanner and re-prompt on failure.
Using literal 5 in loop bounds instead of variable k breaks dynamic input.
→ Use one k variable for the outer bound and both inner loops.
Check these inputs before calling the solution done.
Output is one row of 2k-1 rows numbers; for k=1 you get a single 1.
Outer loop never runs — print nothing or show a message.
k < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows with k rows and 2k-1 rows values per row — fine for labs, noisy for huge k.
Unchecked Scanner leaves k unset — call sc.hasNextInt() first.
Use Math.max(i,j) for cleaner code — see Example 3.
Try these variations to lock in the pattern.
k = 3 or k = 4 — count 2k-1 rows(2k-1 rows)² (e.g. 9×9 = 81 for k=5).print stays on the line; println advances — mix them carefully.k > 0 for interactive programs; k = 1 prints one value.j = 2 so the center value is not duplicated.Quick Takeaway: set k, loop i = k..1, print max(i,j) for left and right halves, then break the row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(k²) | O(1) |
| printRow helper (Example 3) | O(k²) | O(1) |
The concentric number diamond combines top and bottom mirror loops with max(i,j) — a natural step after concentric number squares. Master the fixed-k version first, then try user input and the printRow helper form in Example 3.
Practice the three examples above, then continue to Program 48 for the powers of 11 number pattern.
Every cell uses print — keep println() only after the inner column loop finishes.
print(max(i,j) + " ") and println() after both inner loopsk ≥ 1 for interactive programsScanner return value before using kSystem.out.println() between left and right halves (mid-row break)i = 1 (duplicates center row)j = 1 (duplicates center value)kk = 1 edge casePrint the pattern the beginner-friendly way.
Each cell prints max(i,j)
Definitioni = k..1
Codei = 2..k
Logic2k-1 rows
I/OO(k²)
AnalysisPrint the top half with i = k..1, then mirror with i = 2..k. Each cell uses max(i, j) over left j = k..1 and right j = 2..k — total rows = 2k-1.
Move on to the 1, 11, 121, 1331, 14641 number pattern in the Java number-pattern series.
12 people found this page helpful