Java Palindrome Number Pattern (Increasing-Decreasing)

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

What Is This Pattern?

An increasing-decreasing palindrome pattern starts each row at i, prints an increasing run, then mirrors downward — without repeating the peak digit.

Remember
Rule: for i = 1..rows
        m = i
        print m++   (i times)
        m = m - 2
        print m--   (i-1 times)

1
232
34543
4567654
567898765     ← rows = 5

Unlike Program 51 (running counter with odd/even direction), here each row is self-contained: climb from i, then descend with m = m - 2.

How to Solve It

Start m = i, print increasing, adjust with m = m - 2, then print decreasing.

MethodIdeaBest for
Fixed rowsHard-code rows; dual inner loopsLabs and demos
Scanner inputSame loops; read rows at runtimeInteractive practice
Spaced digitsprint(m + " ") in both loopsEasier-to-read rows

Pseudocode

Pseudocode
for i from 1 to rows:
    m = i
    for j from 1 to i:
        print m; m = m + 1
    m = m - 2
    for k from 1 to i - 1:
        print m; m = m - 1
    new line

Cheat sheet

GoalPattern
Start rowint m = i;
Increasefor (int j = 1; j <= i; j++) print(m++);
Skip peakm = m - 2;
Decreasefor (int k = 1; k < i; k++) print(m--);
End rowSystem.out.println();

Printing Numbers vs Starting a New Line

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

Glue all digits with print, then break once per row.

Live Preview

Change the row count and the palindrome rows update instantly (kept to single digits).

Whole numbers from 3 to 5 (keeps peak digits single). Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · digits = 25
1
232
34543
4567654
567898765

Worked Walkthrough — rows = 5

Trace the increase half, the m = m - 2 step, and the decrease half.

iIncreaseAfter m−2DecreasePrinted row
11(skip)(none)1
22322232
334544334543
4456766544567654
55678988765567898765

After printing 345, m is 6; m = m - 2 lands on 4 so the peak 5 is not printed twice.

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 size — start m = i, increase, m = m - 2, then decrease.

Java
public class IncreasingDecreasingPalindrome {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            int m = i;

            for (int j = 1; j <= i; j++) {
                System.out.print(m++);
            }

            m = m - 2;
            for (int k = 1; k < i; k++) {
                System.out.print(m--);
            }

            System.out.println();
        }
    }
}

How It Works

1. Start at i. Each row begins with m = i — row 3 starts at 3.

2. Climb then adjust. Print m++ for i digits; then m = m - 2 so the peak is not duplicated.

3. Descend. Print m-- for i - 1 digits — row 3 becomes 34543.

Example 2 — Scanner Input

Read rows at runtime. Same dual-loop logic.

Java
import java.util.Scanner;

public class IncreasingDecreasingPalindromeInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
        if (rows < 1) return;

        for (int i = 1; i <= rows; i++) {
            int m = i;
            for (int j = 1; j <= i; j++) System.out.print(m++);
            m = m - 2;
            for (int k = 1; k < i; k++) System.out.print(m--);
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and guard. Read rows; exit early if it is less than 1.

2. Same loops. Only the source of rows changes — the increase / m−2 / decrease structure is 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 — Spaced Digits

Same dual-loop logic — a space after each digit for clearer rows.

Java
public class IncreasingDecreasingPalindromeSpaced {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            int m = i;

            for (int j = 1; j <= i; j++) {
                System.out.print(m++ + " ");
            }

            m = m - 2;
            for (int k = 1; k < i; k++) {
                System.out.print(m-- + " ");
            }

            System.out.println();
        }
    }
}

How It Works

1. Same math. Still increase, m = m - 2, then decrease.

2. Different format. print(m + " ") separates digits so the mirror shape is easier to see.

3. Same width. Row i still has 2i - 1 values — only spacing changes.

Edge Cases & Pitfalls

Check these before calling the solution done.

Forgot m−2

Doubled peak

Without m = m - 2, row 3 prints 345543 instead of 34543.

k <= i

Extra digit

The decrease loop must be k < i (i−1 times). Using k <= i adds one value too many.

m = 1

Wrong start

Each row must start at m = i, not 1 — otherwise every row begins the same way.

rows = 1

Single digit

Output is just 1 — the decrease loop does not run.

rows > 5

Multi-digit values

Without spaces, values like 10 merge into the string. Prefer spaced output or keep rows ≤ 5 for single digits.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
All three examplesO(n²)O(1)

Row i prints 2i − 1 digits; total for n rows is n².

Key Takeaways

  • Rule: start at i, increase i times, m = m - 2, decrease i−1 times.
  • Center: m = m - 2 prevents duplicating the peak digit.
  • Width: row i has 2i − 1 digits.
  • Complexity: O(n²) prints for n rows.

One line: climb from i, skip the peak with m−2, then mirror down.

Frequently Asked Questions

Row 3 prints increasing 345, then after m = m - 2 the decreasing part prints 43 — combined: 34543.
After the increasing loop, m is one past the last printed value. m = m - 2 moves back to the previous digit so the peak is not duplicated.
The decreasing half has i-1 digits — one less than the increasing half to avoid repeating the center.
Yes. Use a variable rows in the outer loop — the same two-loop structure works for any positive n.
Print a space after each digit in both inner loops — see Example 3.
Row i prints 2i - 1 digits — a palindrome-like width that grows each line.
O(n²) for n rows. Row i prints 2i-1 digits; total work grows quadratically.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

Each row starts from i, prints an increasing run of length i, then a decreasing run of length i-1. The key step is m = m - 2 so the peak digit is not duplicated — row 3 gives 34543.

Next: Diagonal Mirror Number Pattern

Continue with the diagonal mirror number pattern in the Java number-pattern series.

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