Java Palindrome Number Triangle Pattern (Outer Peak)
Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A palindrome number triangle prints a symmetric sequence on each row: descend from i to 2, then ascend from 1 to i.
Remember
Rule: for i from 1 to rows,
print j from i down to 2
print j from 1 up to i
1
212
32123
4321234
543212345 ← rows = 5
Unlike Program 36 (right-aligned decreasing sequences), here two inner loops build a mirrored row with no leading spaces.
Approach
How to Solve It
Grow i from 1 to rows; print the left half descending, then the right half ascending.
Method
Idea
Best for
Two inner loops
Descend i..2, then ascend 1..i
Learning, interviews, exams
User-input rows
Same dual-loop logic; read height with Scanner
When triangle size must vary
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 2:
print j
for j from 1 to i:
print j
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (int i = 1; i <= rows; i++)
Left half
for (int j = i; j > 1; j--) System.out.print(j);
Right half
for (int j = 1; j <= i; j++) System.out.print(j);
End the row
System.out.println();
Digits per row
2 * i - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit on the current row
System.out.println
Ends the current line
After both inner loops finish
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the palindrome number triangle updates instantly.
Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 25 digits
1
212
32123
4321234
543212345
Trace
Worked Walkthrough — rows = 5
Trace left half (i..2), right half (1..i), and the full palindrome row.
i
Left
Right
Printed row
1
(empty)
1
1
2
2
12
212
3
32
123
32123
4
432
1234
4321234
5
5432
12345
543212345
Digits per row = 2i − 1. Total for n rows = n².
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and a compact dry-run. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded size — descending left half, then ascending right half.
Java
public class PalindromeNumberTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = i; j > 1; j--)
System.out.print(j);
for (int j = 1; j <= i; j++)
System.out.print(j);
System.out.println();
}
}
}
Output
1
212
32123
4321234
543212345
How It Works
1. Outer loop grows.i runs from 1 to 5 — one longer palindrome each time.
2. Left half descends. Print j from i down to 2 (skipped when i = 1).
3. Right half ascends. Print j from 1 up to i, then println.
When i = 4: 432 + 1234 → 4321234.
Example 2 — Rows Input
Read rows at runtime. Same dual inner loops.
Java
import java.util.Scanner;
public class PalindromeNumberTriangleInput {
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 = i; j > 1; j--)
System.out.print(j);
for (int j = 1; j <= i; j++)
System.out.print(j);
System.out.println();
}
sc.close();
}
}
Output (when user enters 3)
Enter rows: 3
1
212
32123
How It Works
1. Prompt and guard. Read rows; exit early if it is less than 1.
2. Same core. Only the source of rows changes from a literal to user input.
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 = 3
Same dual-loop structure as Example 1 — smaller size for paper tracing.
Java
public class PalindromeNumberTriangleSmall {
public static void main(String[] args) {
int rows = 3;
for (int i = 1; i <= rows; i++) {
for (int j = i; j > 1; j--)
System.out.print(j);
for (int j = 1; j <= i; j++)
System.out.print(j);
System.out.println();
}
}
}
Output
1
212
32123
How It Works
1. Same structure. Descend then ascend — only rows changes from 5 to 3.
2. Three rows.1, then 212, then 32123.
3. Dry-run first. Trace i = 1..3 on paper before coding the full rows = 5 demo.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of digits
If println is inside either inner loop, each digit lands on its own line. Use print for digits; println only after both halves.
Wrong left bound
Broken mirror
Descending to 1 instead of 2 doubles the center 1 and breaks the palindrome.
Skip left half
Only ascending
Omitting the descending loop prints 1, 12, 123 — not a palindrome.
rows = 1
Single digit
Output is just 1 — left loop never runs.
rows ≤ 0
Empty output
The outer loop never runs — print nothing or show a message.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Palindrome triangle (Examples 1–3)
O(n²)
O(1)
Digits = 1 + 3 + 5 + … + (2n−1) = n² — quadratic. Only loop counters are stored.
Remember
Key Takeaways
Rule: for each i, print i..2 then 1..i.
Center is 1: left stops at 2 so the ascending half supplies the single middle 1.
Break the row: call println only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: print i..2, then 1..i, then println().
Frequently Asked Questions
Each row reads the same forward and backward. For example, 4321234 is symmetric around the center digit 1.
First, a loop prints i down to 2 (left half). Then another loop prints 1 up to i (right half). Together they create a mirrored sequence.
The first loop prints 4 3 2, and the second loop prints 1 2 3 4, which together form 4321234.
Row i prints 2i - 1 digits — one more than the previous row.
Program 36 is right-aligned with decreasing sequences. Program 37 builds a symmetric palindrome on each row with two inner loops.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total digits printed are 1 + 3 + 5 + … + (2n-1) = n².
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.
🤔
Did you know?
Each row is a palindrome: print i down to 2, then 1 up to i. Row i prints 2i - 1 digits — total digits across all rows = n².