Main Diagonal
i == j
Left loop prints digit j when row i equals column j.

The diamond diagonal pattern prints Program 53’s diagonal mirror rows for i = 1..n, then mirrors them vertically with a second loop i = n-1..1. For n = 5 you get nine rows forming a full X diamond. This tutorial covers both outer loops, i==j and i==k logic, live preview, worked Java examples, edge cases, and O(n²) complexity.
i == j
Left loop prints digit j when row i equals column j.
i == k
Right loop prints digit k when i == k; runs k = n-1..1.
else " "
All non-diagonal positions print a space — only two digits per row (except overlap).
2nd loop
Second outer loop runs i = n-1..1 — same row logic, reversed order.
3–12 for n
Pick size n and draw the full diamond diagonal pattern instantly in the browser.
Complexity
2n-1 rows × 2n-1 cells per row — total work grows as n².
A diamond diagonal number pattern is Program 53 mirrored vertically. The top half prints rows 1..n; the bottom half repeats rows n-1..1. Row 1 and the last row both show 1 1; the middle row shows a single center digit.
In Java: first outer loop for (i = 1; i <= n; i++), then second for (i = n-1; i >= 1; i--). Each iteration runs the same inner diagonal loops and ends with println().
It combines diagonal row printing with vertical mirroring — a classic diamond pattern technique.
i = 1..n picks each row number.
Left loop prints when i==j; mirror loop prints when i==k.
Two loops per row — main diagonal half, then mirror diagonal half.
Builds on Program 53 diagonal mirror; continue to Program 55 diagonal-fill triangle.
In short: top loop i=1..n, bottom loop i=n-1..1; each row uses i==j and i==k inner loops, then println().
Given n = 5, print nine lines — top half rows 1..5, then bottom half rows 4..1.
// n = 5 (conceptual output)
// 1 1
// 2 2
// 3 3
// 4 4
// 5 | Item | Type | Description |
|---|---|---|
n | int | Pattern size — top-half row count; full output has 2n-1 rows (typically n ≥ 1). |
i, j | int | Row index i; column j for main diagonal, column k for mirror diagonal. |
| Printed output | text | 2n-1 rows total and 2n-1 cells per row — diamond dimensions. |
for i from 1 to n: print diagonal row (i==j, i==k)
for i from n-1 down to 1: print same diagonal row
each row: j loop + k loop, then newline | Approach | Idea | Best for |
|---|---|---|
| Two diagonal loops | if (i==j) print(j) else print(" "); mirror loop with i==k | Fixed width — 2n-1 cells per row |
| Vertical mirror | Second outer loop i = n-1..1 repeats row logic in reverse | Full diamond — 2n-1 rows total |
| Scanner input | sc.nextInt() for n | User-chosen pattern size |
| Star diagonals | Print * on diagonals instead of numbers | Visual X-shape without digits — Example 3 |
| Goal | Pattern |
|---|---|
| Set size | int n = 5; |
| Top half loop | for (i = 1; i <= n; i++) |
| Bottom half loop | for (i = n-1; i >= 1; i--) |
| Main diagonal | for (j = 1; j <= n; j++) — print when i == j |
| Mirror diagonal | for (k = n-1; k >= 1; k--) — print when i == k |
| Non-diagonal cell | System.out.print(" "); |
| Row break | System.out.println(); after both halves |
| Program 53 contrast | Program 53 prints only the top n rows; this adds a second outer loop to mirror vertically |
How outer row selection, main diagonal loop, mirror diagonal loop, and row breaks work together.
for (i = 1; i <= n; i++)Prints rows 1 through n — same as Program 53.
for (i = n-1; i >= 1; i--)Mirrors top half — starts at n-1 to skip duplicate middle row.
j loop: i==j
k loop: i==k (k=n-1..1)Identical inner loops in both outer halves — main and mirror diagonals.
trace n=3Full diamond: 5 rows — top 1..3, bottom 2..1.
Reach for this pattern when teaching vertical mirroring, diagonal conditions, and full diamond output.
Classic follow-up after Program 53 diagonal mirror — adds vertical mirroring.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible pattern size.
Compare with Program 53 (top half only), then continue to Program 55 diagonal-fill triangle.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that locks in vertical mirroring, diagonal conditions, and O(n²) thinking.
Choose pattern size n and draw the full diamond diagonal number pattern in the browser.
Three complete Java programs — fixed n = 5, Scanner input, and a star-diagonal variant. Click View Output to reveal sample console results.
Print nine rows for n = 5 — top half plus mirrored bottom half.
n = 5Hard-coded size — top half loop 1..n, bottom half n-1..1, same diagonal row logic.
public class DiamondDiagonalNumberPattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) System.out.print(j);
else System.out.print(" ");
}
for (int k = n - 1; k >= 1; k--) {
if (i == k) System.out.print(k);
else System.out.print(" ");
}
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (i == j) System.out.print(j);
else System.out.print(" ");
}
for (int k = n - 1; k >= 1; k--) {
if (i == k) System.out.print(k);
else System.out.print(" ");
}
System.out.println();
}
}
} The first outer loop prints rows 1..5 (Program 53 output). The second loop prints rows 4..1, mirroring vertically. Row 1 and row 9 both show 1 1.
Read n with Scanner for flexible output size.
Same diagonal conditions; size n comes from user input.
import java.util.Scanner;
public class DiamondDiagonalNumberPatternInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(i == j ? j : " ");
}
for (int k = n - 1; k >= 1; k--) {
System.out.print(i == k ? k : " ");
}
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
System.out.print(i == j ? j : " ");
}
for (int k = n - 1; k >= 1; k--) {
System.out.print(i == k ? k : " ");
}
System.out.println();
}
sc.close();
}
} Same two outer loops as Example 1; only the size comes from user input.
Print * on diagonals instead of row numbers.
Replace digits with * when i==j or i==k.
public class DiamondDiagonalNumberPatternStar {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(i == j ? "*" : " ");
}
for (int k = n - 1; k >= 1; k--) {
System.out.print(i == k ? "*" : " ");
}
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
System.out.print(i == j ? "*" : " ");
}
for (int k = n - 1; k >= 1; k--) {
System.out.print(i == k ? "*" : " ");
}
System.out.println();
}
}
} Same two outer loops and diagonal logic; digits become * on both halves.
System.out is built in; use Scanner when reading input. Set n (e.g. 5).
for (i = 1; i <= n; i++) — prints n rows (Program 53 output).
for (i = n-1; i >= 1; i--) — mirrors top half; starts at n-1 to skip duplicate middle row.
Each row: j loop with i==j, k loop with i==k, then println().
Total rows = 2n-1; total checks ≈ (2n-1)(2n-1) — O(n²) time, O(1) extra memory.
n = 3 full diamondTrace how top half (rows 1..3) and bottom half (rows 2..1) combine into five output lines.
| Phase | Outer i | Output line |
|---|---|---|
| Top half | 1 | 1 1 |
| Top half | 2 | 2 2 |
| Top half | 3 | 3 |
| Bottom half | 2 | 2 2 |
| Bottom half | 1 | 1 1 |
Five rows total for n=3. Bottom half starts at i=2, not 3, so the middle row is not duplicated.
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 Scanner for dynamic n — see Example 2.
Mirror loop runs k = n-1 down to 1 so the center column is not duplicated.
Example: compare row 3 with n=5 — digits at columns 3 and 3 on main and mirror diagonals.
Practice println vs print for multi-line vs single-line output.
Example: use print("*") when i==j or i==k — see Example 3.
Print * when i==j or i==k instead of the row number.
Example: print n=5 with * on diagonals and compare the X shape.
Fixed width 2n-1 makes O(n²) concrete for beginners.
Example: count cells for n=5 → 9 rows × 9 cells = 81 prints.
Pair the pattern with Scanner and positive-row checks.
Example: reject n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop 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 loop start shows immediately — center column may print twice.
Only loops and console output — no arrays or math libraries.
Change n, use Scanner, or print * on diagonals instead of numbers.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the i==j and i==k conditions first; then try Scanner input and the star variant in Example 3.
Small habits that keep number-pattern code clean.
Use n for pattern size and i/j/k for row/column indices.
ScannerAvoid crashes when the user types letters instead of a number.
Run left diagonal loop, run mirror loop, then println().
Use print(j) or print(" ") in the main loop; same for the mirror loop; one println() per row after both halves.
Trace n = 5, i = 3 on paper — expect digits at both diagonal positions.
Pro Tip: if the center column prints twice, check whether the mirror loop starts at n instead of n-1.
Mistakes that commonly break diamond diagonal number patterns.
Each cell lands on its own line — you get a column, not an X-shaped row.
→ Use print inside both inner loops; println() only after they finish.
Starting the second outer loop at i = n duplicates the middle row — the diamond gets an extra widest line.
→ Start the bottom half at i = n - 1 and count down to 1.
Omitting println() after both inner loops glues all rows onto one line.
→ Always call System.out.println() after both diagonal loops complete.
Starting at k = n duplicates the center column within a row.
→ Start the inner mirror loop at k = n - 1 and count down to 1.
Letters or empty input throw InputMismatchException.
→ Use sc.hasNextInt() before sc.nextInt().
Using literal 5 in loop bounds instead of variable n breaks dynamic input.
→ Use one n variable for both outer loops and inner bounds.
Check these inputs before calling the solution done.
Output is one line: 1 — bottom half loop does not run.
Loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Large values produce wide rows — fine for labs; use smaller n for quick demos.
Unchecked Scanner leaves n unset — call sc.hasNextInt() first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
n = 3, 6, or 8i==j and i==k positions* instead of numbers on diagonalsi = n..1 instead of n-1..1(2n-1)² (e.g. 81 cells for n=5).print stays on the line; println advances — mix them carefully.n > 0 for interactive programs; n = 1 prints a single digit.if (i==j) print(j) else print(" "); mirror: if (i==k) print(k) else print(" ").Quick Takeaway: top loop i=1..n, bottom loop i=n-1..1; each row uses i==j and i==k inner loops, then println().
| Program | Time | Extra space |
|---|---|---|
| Fixed n = 5 (Example 1) | O(n²) | O(1) |
| Scanner input (Example 2) | O(n²) | O(1) |
| Star diagonals (Example 3) | O(n²) | O(1) |
The diamond diagonal pattern combines Program 53 row logic with a vertical mirror loop — a natural step after single-half diagonal patterns. Master the fixed-n version first, then try Scanner input and the star-diagonal variant in Example 3.
Practice the three examples above, then continue to Program 55 for the diagonal-fill number triangle pattern.
Keep bottom half at i = n-1..1 and println() after each row — one break per outer iteration.
1..n, bottom loop n-1..1, and diagonal checks before codingprint(j) or print(" ") in left loop; same for mirror loop; then println()n ≥ 1 for interactive programsScanner return value before using ni = n (duplicates middle row)k = n (duplicates center column)i == j in the mirror loop by mistakenn = 1 edge casePrint Program 53 top half, then mirror rows n-1..1 for a full X diamond.
Full diamond has 2n-1 rows
Top: 1..n; bottom: n-1..1
CodeMain: i==j; mirror: i==k
Logic2n-1 cells/row
O(n²)
AnalysisProgram 54 prints the diagonal mirror pattern (Program 53) for rows 1..n, then repeats the same logic for rows n-1..1 — forming a full diamond with numbers on both diagonals.
Move on to the diagonal-fill number triangle pattern in the Java number-pattern series.
12 people found this page helpful