A palindrome number triangle prints an ascending run 1..i, then mirrors it with i-1..1 on the same line — each row reads the same forwards and backwards.
Remember
Rule: for i from 1 to rows,
print 1..i, then i-1..1
1
121
12321
1234321
123454321 ← rows = 5
Two inner loops do the work. Natural step after Program 26 (diagonal asterisk).
Approach
How to Solve It
Ascend to the peak, then descend from i - 1 — never repeat the middle digit.
Method
Idea
Best for
Two inner loops
Print 1..i, then i-1..1
Learning, interviews, exams
Spaced digits
Same loops; print j + " " / k + " "
When wide rows need gaps
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print j
for k from i - 1 down to 1:
print k
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (int i = 1; i <= rows; i++)
Ascending half
for (int j = 1; j <= i; j++) System.out.print(j);
Mirror half
for (int k = i - 1; k >= 1; k--) System.out.print(k);
End the row
System.out.println();
Spaced digits
System.out.print(j + " "); in both loops
Row length
2 * i - 1 digits
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each ascending and descending digit
System.out.println
Ends the current line
After both inner loops
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the palindrome triangle updates instantly.
Whole numbers from 1 to 9 (single digits keep the shape clean). Tap a chip or type a value.
Live resultrows = 5 · 25 digits
1
121
12321
1234321
123454321
Trace
Worked Walkthrough — rows = 4
Trace each row’s ascending half, mirror half, and full line.
i
Ascending
Mirror
Printed row
1
1
(none)
1
2
12
1
121
3
123
21
12321
4
1234
321
1234321
Digits per row: 2i - 1. Total for n rows: 1 + 3 + … + (2n - 1) = n² — that is why time is O(n²).
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — ascend to i, then mirror from i - 1.
Java
public class PalindromeTriangle {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j);
for (int k = i - 1; k >= 1; k--)
System.out.print(k);
System.out.println();
}
}
}
Output
1
121
12321
1234321
123454321
How It Works
1. Outer loop grows the peak.i is both the row number and the highest digit on that row.
2. Ascending half. Print j from 1 to i.
3. Mirror half. Print k from i - 1 down to 1 — skipping i so the peak appears once.
When i = 3: 123 then 21 → 12321.
Example 2 — Rows Input
Read rows at runtime. Prefer hasNextInt() before nextInt() in real apps.
Java
import java.util.Scanner;
public class PalindromeTriangleInput {
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 = 1; j <= i; j++)
System.out.print(j);
for (int k = i - 1; k >= 1; k--)
System.out.print(k);
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter rows: 4
1
121
12321
1234321
How It Works
1. Prompt and read. Ask for a height, then store it with sc.nextInt().
2. Same two-loop core. Only the outer bound changes from 5 to rows.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive whole number.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive whole number.");
return;
}
Example 3 — Spaced Digits
Keep rows = 5 but print each digit followed by a space in both loops.
Java
public class PalindromeTriangleSpaced {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
for (int k = i - 1; k >= 1; k--)
System.out.print(k + " ");
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. Same bounds. Ascend 1..i and descend i-1..1 exactly as in Example 1.
2. Only the print changes.print(j + " ") and print(k + " ") add gaps between digits.
3. Still a palindrome. The digit sequence is unchanged — only spacing differs.
Edge Cases & Pitfalls
Check these before calling the solution done.
k = i
Double peak
Starting the mirror at i repeats the middle digit (123321 instead of 12321). Use k = i - 1.
Missing mirror
Not a palindrome
Skipping the second loop leaves only 1..i — a growing triangle, not a palindrome.
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 loops.
rows = 1
Single 1
Output is just 1 — the mirror loop never runs. A good sanity check.
rows > 9
Multi-digit
Values like 10 print as two characters and break visual symmetry. Keep rows ≤ 9 for clean single-digit palindromes.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive whole number.
Analysis
Time and Space Complexity
Program
Time
Extra space
Palindrome triangle (Examples 1–3)
O(n²)
O(1)
Total digit prints: 1 + 3 + 5 + … + (2n - 1) = n² — quadratic in n.
Remember
Key Takeaways
Rule: for each i, print 1..i then i-1..1.
Skip the peak: start the mirror at i - 1 so the middle digit appears once.
Break the row: call println only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: print 1..i, mirror with i-1..1, then println().
Frequently Asked Questions
Starting from i − 1 avoids repeating the middle number. For example, for i = 3, printing 123 then 21 makes 12321.
Use System.out.print(j + " ") and System.out.print(k + " ") in the loops instead of System.out.print(j). See Example 3.
The ascending loop prints 1..i and the descending loop prints i − 1..1 — together they read the same forwards and backwards.
System.out.print prints digits on the same line. System.out.println ends the row after both inner loops finish.
One loop handles the ascending half (j) and the other handles the mirror half (k) — clearer than a single complex loop.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because row i prints 2i − 1 digits and the total is 1+3+5+…+(2n−1) = n².
Use sc.hasNextInt() before sc.nextInt(), require rows ≥ 1, and reject non-numeric input — see Example 2 notes.
🤔
Did you know?
This palindrome triangle prints 1..i and then i-1..1 on each row. The second loop mirrors the first, producing outputs like 12321 and 123454321.