Java Palindromic Number Pyramid Pattern

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

What Is This Pattern?

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.

Remember
Rule: for i = 1..rows
        print leading "  " (rows-i+1 times)
        print 1..i
        print i-1..1  (via --n)

          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     ← rows = 5

Unlike Program 55 (left-aligned diagonal fill), here three parts per row — spaces, climb, descend — build a centered palindrome.

How to Solve It

Pad with spaces, print ascending 1..i, then descend with --n for i−1 steps.

MethodIdeaBest for
Fixed rowsHard-code size; three loops per rowLabs and demos
Scanner inputSame logic; read rows at runtimeInteractive practice
Compact rowsStringBuilder without trailing spaceCleaner 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

GoalPattern
Set sizeint rows = 5;
Leading spacesfor (int j = rows; j >= i; j--) print(" ");
Climbfor (int k = 1; k <= i; k++) print(k + " ");
Descendint n = i; for (m = 1; m < i; m++) print(--n + " ");
End rowSystem.out.println();

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineSpaces and each number in the row
System.out.printlnEnds the lineAfter all three parts finish

Build the full centered row with print, then break once.

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 result rows = 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 

Worked Walkthrough — rows = 5, row i = 3

Trace the three parts that build the centered palindrome 1 2 3 2 1.

PartLoopResult
Spacesj = 5..3 → three " "
Climbk = 1..31 2 3
Descendn = 3; --n twice2 1

Combined: 1 2 3 2 1. The peak 3 appears once — descent starts at i−1.

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

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

How It Works

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

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.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–2O(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²).

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.

Next: Hollow Number Pyramid

Continue with the hollow number pyramid pattern in the Java number-pattern series.

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