A diamond diagonal number pattern is Program 53’s X-shape for rows 1..n, then the same rows again from n−1 down to 1 — a full diamond of diagonals.
Remember
Rule: print diagonal-mirror rows for i = 1..n
then again for i = n-1..1
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1 ← n = 5 (9 rows)
Unlike Program 53 (top half only), here a second outer loop mirrors the pattern vertically without duplicating the center row.
Approach
How to Solve It
Reuse the same diagonal row logic twice: once ascending, once descending from n−1.
Method
Idea
Best for
Fixed n
Two outer loops; i==j / i==k
Labs and demos
Scanner input
Same logic; read n at runtime
Interactive practice
Star diagonals
Print * instead of the digit
Visual diamond practice
Pseudocode
Pseudocode
function printRow(i, n):
for j from 1 to n:
if i == j: print j else print space
for k from n-1 down to 1:
if i == k: print k else print space
new line
for i from 1 to n: printRow(i, n)
for i from n-1 down to 1: printRow(i, n)
Cheat sheet
Goal
Pattern
Set size
int n = 5;
Top half
for (int i = 1; i <= n; i++)
Bottom half
for (int i = n - 1; i >= 1; i--)
Main / mirror
Same as Program 53: i==j, i==k
Total rows
2n - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit or space in both halves
System.out.println
Ends the line
After both halves of a row finish
Build each row with print, then break once.
Try it
Live Preview
Change n and the full diamond diagonal pattern updates instantly.
Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · rows = 9
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
Trace
Worked Walkthrough — n = 3
Trace the five rows: top 1..3, then bottom 2..1.
Half
i
Printed row
Top
1
1 1
Top
2
2 2
Top (center)
3
3
Bottom
2
2 2
Bottom
1
1 1
The bottom loop starts at n−1 so the center row (i = n) appears only once.
Code
Java Programs
Three complete programs: fixed n = 5, Scanner input, and star diagonals. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded size — top half 1..n, then bottom half n−1..1.
Java
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();
}
}
}
Output
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
How It Works
1. Top half. Same as Program 53: for each i = 1..n, print on i==j and i==k.
2. Bottom half. Repeat the identical row logic for i = n−1..1 — vertical mirror.
3. No duplicate center. Starting at n−1 skips reprinting the middle row.
Example 2 — Scanner Input
Read n at runtime. Same two outer loops.
Java
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();
if (n < 1) return;
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();
}
}
Output (when user enters 5)
Enter n: 5
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
How It Works
1. Prompt and guard. Read n; exit early if it is less than 1.
2. Same diamond. Only the source of n changes — top then bottom outer loops are identical to Example 1.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int n = sc.nextInt();
if (n < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Star Diagonals
Same full diamond — print * instead of the diagonal digit.
Java
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();
}
}
}
Output
* *
* *
* *
* *
*
* *
* *
* *
* *
How It Works
1. Same positions. Still use i==j and i==k in both outer loops.
2. Different glyph. Print * instead of j/k — the diamond outline stays the same.
3. Same size. Still 2n − 1 rows and 2n − 1 characters per row.
Edge Cases & Pitfalls
Check these before calling the solution done.
Bottom from n
Doubled center
Starting the second loop at i = n reprints the middle row. Always start at n − 1.
k from n
Doubled column
Mirror half must start at k = n − 1 — same rule as Program 53.
Missing spaces
Collapsed X
Skipping else print(" ") packs digits together and destroys the diamond shape.
n = 1
Single digit
Output is just 1 — the bottom loop does not run.
n = 0
Empty output
Neither outer loop runs. Guard interactive input with n >= 1.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
All three examples
O(n²)
O(1)
2n − 1 rows × 2n − 1 characters per row → about 4n² prints, still O(n²).
Remember
Key Takeaways
Rule: Program 53 rows for 1..n, then again for n−1..1.
No duplicate: bottom half starts at n − 1.
Size:2n − 1 rows and 2n − 1 characters each.
Complexity:O(n²) for size n.
One line: print the X top-down, then mirror it upward without repeating the center.
Frequently Asked Questions
Program 53 prints only n rows (the top half). Program 54 adds a second outer loop from n-1 down to 1 to mirror the output vertically.
Starting at n would duplicate the middle row (the widest point). n-1 down to 1 mirrors without repeating the center.
For size n, the diamond has 2n-1 rows: n for the top half and n-1 for the bottom half.
At i=n, both diagonals meet at the same position, so only one digit appears on the center row.
Yes. Use a variable n — both outer loops reuse the same diagonal row logic.
Replace the printed digit with * when i==j or i==k in both outer loops — see Example 3.
O(n²) for (2n-1) rows. Each row checks O(n) cells in two inner loops.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
🤔
Did you know?
Program 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.