Leading Spaces
j = rows..i
Print rows - i + 1 pairs of spaces to center each row.

The palindromic number pyramid prints centered rows that read the same forwards and backwards. For rows = 5: 1, 1 2 1, 1 2 3 2 1, and so on. Each row uses leading spaces, an increase loop 1..i, and a decrease loop i-1..1. This tutorial covers the three-part row logic, live preview, worked Java examples, edge cases, and O(n²) complexity.
j = rows..i
Print rows - i + 1 pairs of spaces to center each row.
k = 1..i
Print numbers 1 through i on the way up.
--n
Set n = i, then print --n for i-1 steps — mirrors without duplicating peak.
2i - 1 nums
Row i has 2i-1 numbers — increase half plus decrease half.
3–12 rows
Pick row count and draw the palindromic pyramid instantly in the browser.
Complexity
Each row prints O(n) spaces and numbers — total work grows as n².
A palindromic number pyramid centers each row with leading spaces, then prints 1..i and i-1..1. Row 2 reads 1 2 1; row 3 reads 1 2 3 2 1.
In Java: outer for (i = 1; i <= rows; i++), space loop j = rows..i, increase loop k = 1..i, decrease loop with --n, then println().
Given rows = 5, print five centered palindromic rows — widest row has 1 2 3 4 5 4 3 2 1.
// rows = 5 (conceptual output — centered)
// 1
// 1 2 1
// 1 2 3 2 1
// 1 2 3 4 3 2 1
// 1 2 3 4 5 4 3 2 1 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle rows (typically ≥ 1). |
i, j, k, n, m | int | Row i; space index j; increase k; decrease n/m. |
| Printed output | text | 2i-1 numbers per row when centered; palindromic sequence. |
for i from 1 to rows:
print (rows-i+1) space pairs
print 1..i
print i-1..1 using --n
newline | Approach | Idea | Best for |
|---|---|---|
| Three-part row | Spaces + increase loop + decrease loop | Centered palindromic rows |
| StringBuilder row | Build row without trailing spaces | Cleaner console output — Example 3 |
| Scanner input | sc.nextInt() for rows | User-chosen row count |
| Compact output | StringBuilder joins values with single spaces | No trailing space per row — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | int rows = 5; |
| Outer loop | for (i = 1; i <= rows; i++) |
| Space loop | for (j = rows; j >= i; j--) print(" ") |
| Increase loop | for (k = 1; k <= i; k++) print(k) |
| Decrease loop | n = i; print(--n) for m = 1..i-1 |
| Row break | System.out.println(); after all three parts |
| Program 55 contrast | Diagonal-fill triangle uses step logic; this pyramid uses centered palindrome rows |
How leading spaces, increase loop, decrease loop, and row breaks work together.
for (j = rows; j >= i; j--)
print(" ")Centers row i in the pyramid.
for (k = 1; k <= i; k++)
print(k)Prints 1 2 3 ... i on row i.
n = i
for (m = 1; m < i; m++)
print(--n)Prints i-1 ... 1 — mirrors without repeating peak.
trace i=3, rows=5Dry-run row 3: spaces + 1 2 3 + 2 1 → palindrome.
Reach for this pattern when teaching centering, palindrome rows, and nested loops.
Classic follow-up after diagonal-fill triangles — introduces centered palindromic rows.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible pattern size.
Compare with Program 55 (diagonal-fill triangle), then continue to Program 57 hollow pyramid.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that locks in centering, palindrome rows, and O(n²) thinking.
Choose pattern size n and draw the full palindromic number pyramid number pattern in the browser.
Three complete Java programs — fixed rows = 5, Scanner input, and a compact StringBuilder variant. Click View Output to reveal sample console results.
Print five centered palindromic rows — widest row has nine numbers.
rows = 5Hard-coded rows = 5 — leading spaces, increase loop 1..i, decrease loop with --n.
public class PalindromicNumberPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--) System.out.print(" ");
for (int k = 1; k <= i; k++) System.out.print(k + " ");
int n = i;
for (int m = 1; m < i; m++) System.out.print(--n + " ");
System.out.println();
}
}
} Row 1 prints one centered 1. Row 3 prints spaces, then 1 2 3, then 2 1 — a palindrome.
Read rows with Scanner for flexible output size.
Same palindromic row logic; row count comes from user input.
import java.util.Scanner;
public class PalindromicNumberPyramidInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--) System.out.print(" ");
for (int k = 1; k <= i; k++) System.out.print(k + " ");
int n = i;
for (int m = 1; m < i; m++) System.out.print(--n + " ");
System.out.println();
}
sc.close();
}
} Same palindromic row logic as Example 1; row count comes from Scanner input.
Build each row with StringBuilder — no trailing space.
Same logic; use StringBuilder to join values without a trailing space.
public class PalindromicNumberPyramidCompact {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
StringBuilder row = new StringBuilder();
for (int j = rows; j >= i; j--) row.append(" ");
for (int k = 1; k <= i; k++) {
if (row.length() > 0 && !row.toString().endsWith(" ")) row.append(" ");
row.append(k);
}
int n = i;
for (int m = 1; m < i; m++) {
row.append(" ").append(--n);
}
System.out.println(row);
}
}
} Same palindromic logic; StringBuilder produces clean rows without trailing spaces.
Set rows (e.g. 5). Outer loop for (i = 1; i <= rows; i++) builds one centered row per iteration.
for (j = rows; j >= i; j--) prints " " to center row i.
for (k = 1; k <= i; k++) prints 1 2 3 ... i.
n = i, then print(--n) for m = 1..i-1; then println().
Total numbers = n² (e.g. 25 for rows=5) — O(n²) time, O(1) extra memory.
i = 3, rows = 5Trace row 3 to see spaces, increase half, and decrease half form 1 2 3 2 1.
| Phase | Loop | Row so far |
|---|---|---|
| Spaces | j=5..3 | |
| Increase | k=1..3 | 1 2 3 |
| Decrease | m=1,2 → --n | 1 2 3 2 1 |
Final centered row 3: 1 2 3 2 1. Then println() moves to row 4.
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 rows — see Example 2.
Each row prints rows - i + 1 pairs of spaces before numbers.
Example: trace row 3 with rows=5 — 2 space pairs before digits.
Practice println vs print for multi-line vs single-line output.
Example: use StringBuilder for clean rows — see Example 3.
Each row reads the same forwards and backwards — peak digit appears once.
Example: row 4 reads 1 2 3 4 3 2 1 when centered.
Each row prints O(n) spaces and numbers — total work grows as n².
Example: count numbers on row 5 → 2×5-1 = 9 digits per row.
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 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.
Missing decrease loop shows immediately — rows are not palindromic.
Only loops and console output — no arrays or math libraries.
Change rows, use Scanner, or build rows with StringBuilder.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the three-part row (spaces, up, down) first; then try Scanner input and the StringBuilder variant in Example 3.
Small habits that keep number-pattern code clean.
Use rows for height and i/j/k/res for loop variables.
ScannerAvoid crashes when the user types letters instead of a number.
Finish the inner loop for row i, then call println().
Set n = i before the decrease loop; print --n exactly i-1 times.
Trace rows = 5, i = 3 on paper — expect centered 1 2 3 2 1.
Pro Tip: if the peak digit prints twice, check whether the decrease loop uses m < i.
Mistakes that commonly break palindromic number pyramid number patterns.
Each number lands on its own line — you get a vertical stack, not a centered pyramid row.
→ Use print inside space, increase, and decrease loops; println() only after they finish.
Without --n, rows print only 1..i — no palindrome.
→ Add the decrease loop: n = i, then print(--n) for m = 1..i-1.
Omitting println() after both inner loops glues all rows onto one line.
→ Always call System.out.println() after space, increase, and decrease loops complete.
Looping m = 1..i duplicates the peak digit — row 3 becomes 1 2 3 3 2 1.
→ Use m < i so exactly i-1 descending digits print.
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 rows variable for outer loop and k initialization.
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.
rows < 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 rows 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 reads the same forwards and backwards--n loop and see non-palindromic rowsi has 2i-1 numbers; total = n² (e.g. 25 numbers for rows=5).print stays on the line; println advances — mix them carefully.rows > 0 for interactive programs; rows = 1 prints one number.1..i, then --n for i-1 steps.Quick Takeaway: outer i=1..rows; spaces; print 1..i; print --n; then println().
| Program | Time | Extra space |
|---|---|---|
| Fixed rows = 5 (Example 1) | O(n²) | O(1) |
| Scanner input (Example 2) | O(n²) | O(1) |
| Compact rows (Example 3) | O(n²) | O(1) |
The palindromic number pyramid combines centering with increase/decrease loops — a natural step after diagonal-fill triangle patterns. Master the fixed-rows version first, then try Scanner input and the compact StringBuilder variant in Example 3.
Practice the three examples above, then continue to Program 57 for the hollow number pyramid pattern.
Print leading spaces before numbers on every row — one println() per outer iteration.
i, space loop, increase loop, and decrease loop before coding1..i, then --n; then println()rows ≥ 1 for interactive programsScanner return value before using rowsm <= i in decrease loop (duplicates peak)rowsrows = 1 edge caseCenter each row with spaces, then print increase and decrease halves.
Row i has 2i-1 numbers
Definitioni = 1..rows
CodePrint 1..i then --n
Logic2i-1 nums/row
O(n²)
AnalysisEach row is palindromic: print leading spaces, numbers 1..i, then i-1..1. Row 3 reads 1 2 3 2 1 when centered.
Move on to the hollow number pyramid pattern in the Java number-pattern series.
12 people found this page helpful