Java Palindrome Number Triangle Pattern (Outer Peak)

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

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.

How to Solve It

Grow i from 1 to rows; print the left half descending, then the right half ascending.

MethodIdeaBest for
Two inner loopsDescend i..2, then ascend 1..iLearning, interviews, exams
User-input rowsSame dual-loop logic; read height with ScannerWhen 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

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Left halffor (int j = i; j > 1; j--) System.out.print(j);
Right halffor (int j = 1; j <= i; j++) System.out.print(j);
End the rowSystem.out.println();
Digits per row2 * i - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit on the current row
System.out.printlnEnds the current lineAfter both inner loops finish

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

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 result rows = 5 · 25 digits
1
212
32123
4321234
543212345

Worked Walkthrough — rows = 5

Trace left half (i..2), right half (1..i), and the full palindrome row.

iLeftRightPrinted row
1(empty)11
2212212
33212332123
443212344321234
5543212345543212345

Digits per row = 2i − 1. Total for n rows = n².

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

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

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

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.

Time and Space Complexity

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

Digits = 1 + 3 + 5 + … + (2n−1) = n² — quadratic. Only loop counters are stored.

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

Next: Sequential Triangle 1..15

Move on to the sequential number triangle with decreasing width in the Java number-pattern series.

Program 38 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