A palindromic number pyramid centers each row with leading spaces, then prints 1..i followed by i−1..1 — every row reads the same forwards and backwards.
Unlike Program 55 (left-aligned diagonal fill), here three parts per row — spaces, climb, descend — build a centered palindrome.
Approach
How to Solve It
Pad with spaces, print ascending 1..i, then descend with --n for i−1 steps.
Method
Idea
Best for
Fixed rows
Hard-code size; three loops per row
Labs and demos
Scanner input
Same logic; read rows at runtime
Interactive practice
Compact rows
StringBuilder without trailing space
Cleaner console output
Pseudocode
Pseudocode
for i from 1 to rows:
for j from rows down to i:
print " "
for k from 1 to i:
print k
n = i
for m from 1 to i-1:
n = n - 1
print n
new line
Cheat sheet
Goal
Pattern
Set size
int rows = 5;
Leading spaces
for (int j = rows; j >= i; j--) print(" ");
Climb
for (int k = 1; k <= i; k++) print(k + " ");
Descend
int n = i; for (m = 1; m < i; m++) print(--n + " ");
End row
System.out.println();
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Spaces and each number in the row
System.out.println
Ends the line
After all three parts finish
Build the full centered row with print, then break once.
Try it
Live Preview
Change the row count and the palindromic pyramid updates instantly.
Whole numbers from 3 to 8. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · peak = 5
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
Trace
Worked Walkthrough — rows = 5, row i = 3
Trace the three parts that build the centered palindrome 1 2 3 2 1.
Part
Loop
Result
Spaces
j = 5..3 → three " "
Climb
k = 1..3
1 2 3
Descend
n = 3; --n twice
2 1
Combined: 1 2 3 2 1. The peak 3 appears once — descent starts at i−1.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and compact StringBuilder rows. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded size — leading spaces, climb 1..i, descend with --n.
Java
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();
}
}
}
Output
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
How It Works
1. Center. Print " " from rows down to i so each row sits under the previous one.
2. Climb. Print 1..i with a space after each number.
3. Descend. Set n = i, then print --n exactly i−1 times — mirrors without repeating the peak.
Example 2 — Scanner Input
Read rows at runtime. Same three-part row logic.
Java
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();
if (rows < 1) return;
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();
}
}
1. Prompt and guard. Read rows; exit early if it is less than 1.
2. Same pyramid. Only the source of rows changes — spaces, climb, and descend stay identical.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Compact Rows
Same logic — StringBuilder joins values without a trailing space after the last digit.
Java
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);
}
}
}
Output
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
How It Works
1. Same shape. Still pad, climb, and descend — the pyramid looks the same.
2. Smarter joins. Spaces go between numbers only; leading indent stays as " " pairs.
3. No trailing space. The last digit ends the line cleanly before println.
Edge Cases & Pitfalls
Check these before calling the solution done.
Skip descend
Not a palindrome
Without the --n loop, row 3 is only 1 2 3 — not 1 2 3 2 1.
print(n--)
Doubled peak
Use --n (pre-decrement). Starting from i with post-decrement would reprint the peak.
One space
Off-center
Padding uses " " (two spaces) so numbers with trailing spaces still align as a pyramid.
rows = 1
Single value
Output is a centered 1 — the descend loop does not run.
rows = 0
Empty output
The outer loop never runs. Guard interactive input with rows >= 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
Examples 1–2
O(n²)
O(1)
StringBuilder (Example 3)
O(n²)
O(n) per row
Row i prints O(n) spaces plus 2i − 1 numbers → overall O(n²).
Remember
Key Takeaways
Rule: spaces → 1..i → i−1..1 with --n.
Peak once: descend starts after i, so the middle digit is not duplicated.
Width: row i has 2i − 1 numbers.
Complexity:O(n²) for n rows.
One line: pad, climb to i, then mirror down — each row is a centered palindrome.
Frequently Asked Questions
Each row reads the same forwards and backwards — e.g. 1 2 3 2 1. Leading spaces center the pyramid shape.
Row 2 prints increasing 1 2, then decreasing 1 using the --n loop — forming a palindrome.
One loop prints 1..i, another prints i-1..1 with --n. Together they mirror the row without duplicating the peak.
Yes. Use a variable rows in all loop bounds and space padding.
2i - 1 numbers — i ascending plus i-1 descending.
Build each row with StringBuilder — see Example 3.
O(n²) for n rows because each row prints O(n) spaces and numbers.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
🤔
Did you know?
Each row is palindromic: print leading spaces, numbers 1..i, then i-1..1. Row 3 reads 1 2 3 2 1 when centered.