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

The diagonal mirror pattern prints numbers on the main diagonal and mirror diagonal with spaces elsewhere — forming an X shape. For n = 5: 1 1, 2 2, 3 3. This tutorial covers 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).
2n - 1 cells
Each row has exactly 2n-1 character positions for size n.
3–12 for n
Pick a size n and draw the diagonal mirror pattern instantly in the browser.
Complexity
Each row checks 2n-1 cells — total work grows as n².
A diagonal mirror number pattern places digits only on the main diagonal (i == j) and mirror diagonal (i == k). Row 1 shows 1 1; row 3 shows digits at both X arms.
In Java: outer for (i = 1; i <= n; i++), inner for (j = 1; j <= n; j++) print digit or space, inner for (k = n-1; k >= 1; k--) print digit or space, then println() after both halves.
It is a nested-loop exercise that places digits only on the main and mirror diagonals.
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.
Follow Program 52 palindrome rows; continue to Program 54 diamond diagonal.
In short: outer i=1..n, inner j=1..n with i==j, inner k=n-1..1 with i==k, then println().
Given n = 5, print five lines with digits on both diagonals and spaces elsewhere.
// n = 5 (conceptual output)
// 1 1
// 2 2
// 3 3
// 4 4
// 5 | Item | Type | Description |
|---|---|---|
n | int | Pattern size — number of rows and diagonal width (typically ≥ 1). |
i, j | int | Row index i; column j for main diagonal, column k for mirror diagonal. |
| Printed output | text | 2n-1 cells per row — fixed width for size n. |
for i from 1 to n:
for j from 1 to n: print j if i==j else space
for k from n-1 down to 1: print k if i==k else space
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 |
| 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; |
| Outer loop | for (i = 1; i <= n; 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 52 contrast | Palindrome rows use increase/decrease loops; this pattern uses i==j and i==k diagonal checks |
How outer row selection, main diagonal loop, mirror diagonal loop, and row breaks work together.
for (i = 1; i <= n; i++)Picks row number i — also the diagonal digit value.
for (j = 1; j <= n; j++)
if (i == j) print(j)
else print(" ")Prints digit at column j when i == j.
for (k = n-1; k >= 1; k--)
if (i == k) print(k)
else print(" ")Prints digit when i == k; loop starts at n-1.
trace i=3, n=5Dry-run row 3 — digits at both diagonal positions: 3 3.
Reach for this pattern when teaching nested loops, diagonal conditions, and fixed-width row output.
Classic follow-up after palindrome row patterns like Program 52.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible pattern size.
Compare with Program 52 (palindrome rows), then continue to Program 54 (diamond diagonal).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in i==j, i==k, mirror bounds, and O(n²) thinking.
Choose pattern size n and draw the diagonal mirror 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 five rows with digits on main and mirror diagonals — X-shaped output.
n = 5Hard-coded size — left loop with i==j, mirror loop with i==k.
public class DiagonalMirrorNumberPattern {
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();
}
}
} When i = 3 and n = 5, the main diagonal prints 3 at column 3; the mirror diagonal also prints 3. Row 1 prints 1 on both diagonals — 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 DiagonalMirrorNumberPatternInput {
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();
}
sc.close();
}
} Identical diagonal logic to Example 1; only the size is dynamic.
Print * on diagonals instead of row numbers.
Replace digits with * when i==j or i==k.
public class DiagonalMirrorNumberPatternStar {
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();
}
}
} Same diagonal logic; only the printed character changes from digit to *.
System.out is built in; use Scanner when reading input. Set n (e.g. 5).
for (i = 1; i <= n; i++) — one iteration per output line.
Left loop: print digit when i==j, else space. Mirror loop: print when i==k for k = n-1..1.
System.out.println(); after both inner loops moves to the next row.
Total checks = n(2n-1) — O(n²) time, O(1) extra memory.
i = 3, n = 5Trace row 3 to see how main and mirror diagonal checks place digits at both X arms.
| Half | Condition hit | Row so far |
|---|---|---|
| Left (j=1..2) | no match | |
| Left (j=3) | i==j → print 3 | 3 |
| Left (j=4,5) | spaces | 3 |
| Mirror (k=4,5) | spaces | 3 |
| Mirror (k=3) | i==k → print 3 | 3 3 |
| Mirror (k=2,1) | spaces | 3 3 |
Final row: 3 3. Then println() moves to the next row.
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 → 5 rows × 9 cells = 45 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 diagonal mirror 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 at k = n duplicates the center column — row 3 shows two middle digits instead of one spaced pair.
→ Start the mirror loop at k = 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.
Using i == j in the mirror loop prints digits on the wrong diagonal.
→ Main loop uses i == j; mirror loop uses i == k.
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 inner loop bounds.
Check these inputs before calling the solution done.
Output is one line: 1 — mirror 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 diagonalsk = n..1 instead of n-1..1n(2n-1) (e.g. 45 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: outer i=1..n, inner j=1..n with i==j, inner k=n-1..1 with i==k, 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 diagonal mirror pattern combines nested loops with diagonal conditions — a natural step after palindrome row 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 54 for the diamond diagonal number pattern.
Keep println() after both diagonal halves — one row break per outer iteration.
i, main diagonal i == j, and mirror diagonal i == k before codingprint(j) or print(" ") in left loop; same for mirror loop; then println()n ≥ 1 for interactive programsScanner return value before using nk = n (duplicates center)i == j in the mirror loop by mistakenn = 1 edge casePrint digits only where the main and mirror diagonals cross each row.
Each row prints 2n-1 cells
i = 1..n
CodeMain: i==j; mirror: i==k
Logic2n-1 cells/row
O(n²)
AnalysisRow i prints the number on the main diagonal (i == j) and on the mirror diagonal (i == k). All other cells are spaces — an X-shaped number pattern.
Move on to the diamond diagonal number pattern in the Java number-pattern series.
12 people found this page helpful