Shape Rule
Odd-width rows
Row i prints digits 1..(2*i-1) concatenated; leading spaces center each row.

The centered number diamond prints 1, then 123, then 12345, and so on — then mirrors back down to 1. This tutorial covers the top and bottom halves, digit loops, live preview, algorithm steps, worked Java examples, edge cases, and complexity.
Odd-width rows
Row i prints digits 1..(2*i-1) concatenated; leading spaces center each row.
Center rows
for (j = i; j < rows; j++) prints leading spaces before digits.
1..2*i-1
System.out.print(k) for k = 1..2*i-1 — concatenated digits per row.
Bottom half
for (i = rows-1; i >= 1; i--) repeats the same row logic downward.
1–20 rows
Pick a row count and draw the centered number diamond instantly in the browser.
Complexity
Prints odd-width rows mirrored across n rows — n² iterations; extra memory stays O(1).
A centered number diamond prints 1, then 123, then 12345, up to the widest row, then mirrors back down. With rows = 5, the middle line is 123456789.
In Java you use a top-half loop (i = 1..rows), leading spaces, and a digit loop (k = 1..2*i-1), then a bottom-half loop mirrors the same logic with i = rows-1..1.
It combines centering spaces with a mirrored second loop — the classic two-triangle diamond pattern.
Grow with i = 1..rows, then mirror with i = rows-1..1.
rows - i spaces center each row in the diamond.
Each row prints 1, 123, 12345, … without separators.
Follow Program 43 right-aligned triangle; continue to Program 45 star cross pattern.
In short: top half grows odd-width digit rows with leading spaces; bottom half mirrors the same logic; call System.out.println() after each row.
Given a positive integer rows (e.g. 5), print a centered number diamond: odd-width digit rows grow to the middle, then mirror downward.
// rows = 5 (conceptual shape)
// 1
// 123
// 12345
// 1234567
// 123456789
// 1234567
// 12345
// 123
// 1 | Item | Type | Description |
|---|---|---|
rows | int | Height of the top half (and widest row index). |
| Printed output | text | Centered diamond of concatenated digits; 2*rows - 1 total lines. |
for i from 1 to rows (top half):
print (rows - i) spaces
for k from 1 to 2*i-1: print k
print newline
for i from rows-1 down to 1 (bottom half):
print (rows - i) spaces
for k from 1 to 2*i-1: print k
print newline | Approach | Idea | Best for |
|---|---|---|
| Top + bottom halves | Leading spaces, then digits 1..2*i-1 | Learning and interviews |
| User-input size | sc.nextInt(); | Flexible console programs |
| Spaced-digit diamond | Print k + " " between digits | More readable output |
| Goal | Pattern |
|---|---|
| Space loop | for (j = i; j < rows; j++) (top) or j = rows; j > i; j-- (bottom) |
| Top half | for (i = 1; i <= rows; i++) |
| Bottom half mirror | for (i = rows-1; i >= 1; i--) |
| Digit loop | for (k = 1; k < i * 2; k++) + print(k) |
| End the row | System.out.println(); |
| Program 43 contrast | Right-aligned triangle grows one way; this pattern mirrors rows after the widest line |
Same diamond row — how centering spaces and concatenated digits work together.
i = 1..rowsGrowing half — widest row at i = rows
i = rows-1..1Mirrors top half after the middle row
k = 1..2*i-1Prints 1, 123, 12345, … concatenated
trace i=3Dry-run row 3: two spaces then 12345
Reach for this pattern when teaching symmetry — two mirrored loop halves with centering spaces.
Classic follow-up after right-aligned triangles and pyramids.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 43 (right-aligned triangle), then continue to Program 45 (star cross).
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(n²) thinking.
Choose a row count between 1 and 20 and draw the centered number diamond in the browser.
Three complete Java programs — fixed row count, Scanner input, and a spaced-digit variant. Click View Output to reveal sample console results.
Print a full diamond with top and bottom loop pairs.
rows = 5Hard-coded size — top half grows, bottom half mirrors each row.
public class NumberDiamondPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k);
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k);
}
System.out.println();
}
}
} When i = 1, four leading spaces precede a single 1. When i = 3, two spaces precede 12345 — odd-width rows build the diamond shape.
Let the user choose the height at runtime.
Read the row count with Scanner.nextInt() (check hasNextInt() in real apps).
import java.util.Scanner;
public class NumberDiamondPatternInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k);
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k);
}
System.out.println();
}
sc.close();
}
} Same nested-loop core as Example 1; only the source of rows changes. Non-numeric input throws InputMismatchException with nextInt() — check hasNextInt() for safer labs.
Add spaces between digits for a more readable diamond.
Print a space after each digit for a more readable centered diamond.
public class SpacedNumberDiamond {
public static void main(String[] args) {
int rows = 3;
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k + " ");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k + " ");
}
System.out.println();
}
}
} Same two-loop structure; print(k + " ") adds spacing between digits and wider indent pairs keep centering.
System.out is built in; use Scanner when reading input. Set rows (fixed or from input).
for (i = 1; i <= rows; i++) — walks the growing top half of the diamond.
for (j = i; j < rows; j++) prints leading spaces before digits.
for (i = rows-1; i >= 1; i--) repeats the same row logic downward.
Total digits printed grow on the order of n² — O(n²) time, O(1) extra memory.
rows = 5, top-half row i = 3Trace one growing row before the mirror loop runs.
| Step | Loop | Prints |
|---|---|---|
| Spaces | j = 3, 4 (2 times) | |
| Digits | k = 1..5 | 12345 |
Row output: 12345 — full diamond has 2*rows - 1 lines when the bottom half mirrors the top.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: skip the bottom half and get a pyramid instead of a diamond.
Foundation for number diamonds, centered pyramids, and mirrored patterns.
Example: swap digits for stars to build a star diamond variant.
Practice System.out.print vs row newline without complex math.
Example: put System.out.println() inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print 1, 12, 123 without spaces between digits.
Triangular totals make O(n²) concrete for beginners.
Example: count output lines for n = 5 → 9 (2*5-1).
Pair the pattern with Scanner and positive-row checks.
Example: reject rows <= 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 loop bounds show up immediately as a lopsided or off-center diamond.
Only loops and console output — no arrays or math libraries.
Invert, center, left-align, or swap digits for stars with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the two-loop diamond first; then try the spaced-digit variant in Example 3.
Small habits that keep number-pattern code clean.
Use rows (or n) 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.
One-liner for spaces: System.out.print(" ".repeat(rows - i)); before the digit loop.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of digits per line, you almost certainly put System.out.println() inside the inner loop.
Mistakes that commonly break centered number diamonds.
Each digit lands on its own line — you get a column, not a diamond.
→ Use System.out.print for spaces and numbers; System.out.println() only after both inner loops.
Printing only the top loop gives a pyramid, not a full diamond.
→ Add for (i = rows-1; i >= 1; i--) after the top-half loop.
Omitting System.out.println() glues every row onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input throw InputMismatchException.
→ Prefer Scanner and re-prompt on failure.
Using k <= i instead of k < i * 2 prints too few digits per row.
→ Keep for (k = 1; k < i * 2; k++) for odd-width rows 1, 3, 5, …
Check these inputs before calling the solution done.
Output is one centered 1 on a single line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as odd-width rows mirrored plus spaces — fine for labs, noisy for huge n.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
Print k + " " between digits — see Example 3.
Try these variations to lock in the pattern.
print(k + " ") like Example 3" " when needed2*rows-1 digits; the full diamond has 2*rows-1 lines.print stays on the line; println advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print one indented 1.System.out.print(k) to concatenate digits; add spaces in Example 3 when readability matters.Quick Takeaway: top half grows odd-width rows, bottom half mirrors, leading spaces center each line, then break the row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Spaced-digit diamond (Example 3) | O(rows²) | O(1) |
The centered number diamond combines nested loops with a simple mirror pattern — a natural step after right-aligned triangles. Master the fixed-rows version first, then try user input and the spaced-digit diamond.
Practice the three examples above, then continue to Program 45 for the star cross pattern with 0s.
Every row prints 2*i-1 digits — keep println() only after both inner loops finish.
print(k) and println() after each rowrows ≥ 1 for interactive programsScanner return value before using rowsSystem.out.println() inside the inner digit loopk <= i instead of k < i * 2rows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints 1..2*i-1
Definition2*i-1 digits
CodeMirror i down
Logic2*rows - 1
I/OO(n²) time
AnalysisEach row prints digits 1..(2*i-1) concatenated without spaces. Leading spaces center the shape; a second loop mirrors the top half downward.
Move on to the star cross pattern with 0s in the Java number-pattern series.
12 people found this page helpful