Java Palindrome Number Triangle Pattern

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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).

How to Solve It

Ascend to the peak, then descend from i - 1 — never repeat the middle digit.

MethodIdeaBest for
Two inner loopsPrint 1..i, then i-1..1Learning, interviews, exams
Spaced digitsSame 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

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Ascending halffor (int j = 1; j <= i; j++) System.out.print(j);
Mirror halffor (int k = i - 1; k >= 1; k--) System.out.print(k);
End the rowSystem.out.println();
Spaced digitsSystem.out.print(j + " "); in both loops
Row length2 * i - 1 digits

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach ascending and descending digit
System.out.printlnEnds the current lineAfter both inner loops

Print digits without a newline, then end the row once.

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 result rows = 5 · 25 digits
1
121
12321
1234321
123454321

Worked Walkthrough — rows = 4

Trace each row’s ascending half, mirror half, and full line.

iAscendingMirrorPrinted row
11(none)1
2121121
31232112321
412343211234321

Digits per row: 2i - 1. Total for n rows: 1 + 3 + … + (2n - 1) = n² — that is why time is O(n²).

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();
        }
    }
}

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();
    }
}

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();
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra space
Palindrome triangle (Examples 1–3)O(n²)O(1)

Total digit prints: 1 + 3 + 5 + … + (2n - 1) = n² — quadratic in n.

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.

Next: 0-Centered Descending Mirror

Move on to the 0-centered descending mirror in the Java number-pattern series.

Program 28 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful