Java Number Pattern (Decreasing then Increasing)

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

What Is This Pattern?

A decreasing-increasing number pattern builds each row from two parts: a descending prefix (i..2) and an ascending suffix (1..(rows+1−i)). Every row has exactly rows digits.

Remember
Rule: for i = 1..rows
        print j from i down to 2
        print k from 1 to (rows+1-i)

12345
21234
32123
43212
54321     ← rows = 5

Unlike Program 49 (products i×j), here two complementary loops shift a pivot across a fixed-width row.

How to Solve It

For each row i, run a decreasing loop first, then an increasing loop — then break the line.

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

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i down to 2:
        print j
    for k from 1 to (rows + 1 - i):
        print k
    new line

Cheat sheet

GoalPattern
Set sizeint rows = 5;
Walk rowsfor (int i = 1; i <= rows; i++)
Decreasing partfor (int j = i; j > 1; j--)
Increasing partfor (int k = 1; k <= rows + 1 - i; k++)
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 decreasing-increasing pattern updates instantly.

Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · digits = 25
12345
21234
32123
43212
54321

Worked Walkthrough — rows = 5

Trace how the decreasing prefix and increasing suffix combine on each row.

iDecreasingIncreasingPrinted row
1(none)1..512345
221..421234
3321..332123
44321..243212
55432154321

Row 1 skips the decreasing loop (j = 1; j > 1 is false) — only the suffix prints.

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 — decreasing loop, then increasing loop, then a line break.

Java
public class DecreasingIncreasingNumberPattern {
    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 k = 1; k <= (rows + 1 - i); k++) {
                System.out.print(k);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer loop. i is the row number and the start of the decreasing prefix.

2. Two inner loops. First print j from i down to 2; then print k from 1 to rows+1−i.

3. Fixed width. Together both loops always print exactly rows digits before println().

Example 2 — Scanner Input

Read rows at runtime. Same dual-loop logic.

Java
import java.util.Scanner;

public class DecreasingIncreasingNumberPatternInput {
    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++) {
            for (int j = i; j > 1; j--) {
                System.out.print(j);
            }
            for (int k = 1; k <= (rows + 1 - i); k++) {
                System.out.print(k);
            }
            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 dual-loop 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 DecreasingIncreasingNumberPatternSpaced {
    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 k = 1; k <= (rows + 1 - i); k++) {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }
}

How It Works

1. Same bounds. Still print decreasing j then increasing k.

2. Different format. print(digit + " ") separates values so the pivot is easier to see.

3. Same shape. Each row still has exactly rows numbers — only spacing changes.

Edge Cases & Pitfalls

Check these before calling the solution done.

Skip decreasing

Wrong first row

Using j >= 1 instead of j > 1 adds an extra 1 before the suffix on every row.

Wrong bound

Uneven rows

The increasing limit must be rows + 1 - i. Using i or rows alone breaks the fixed width.

Missing println

One long line

Without println() after both loops, every digit prints on a single horizontal line.

rows = 1

Single digit

Output is just 1.

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
All three examplesO(n²)O(1)

Each of n rows prints exactly n digits → n² prints total.

Key Takeaways

  • Rule: decreasing i..2, then increasing 1..(rows+1−i).
  • Width: every row has exactly rows digits — the pivot shifts.
  • Spacing: use print for digits and one println after both loops.
  • Complexity: O(n²) prints for n rows.

One line: for each i, print down from i to 2, then up from 1 to rows+1−i.

Frequently Asked Questions

Row i prints j from i down to 2, then k from 1 up to (rows+1-i). Concatenating both parts creates lines like 21234 and 32123.
For i=1, the decreasing loop (j=i..2) does not run, so only the increasing loop prints 1..rows.
For i=3, decreasing prints 32, increasing prints 123 (rows+1-i=3), giving 32123.
Yes. Use a variable rows — the same two-loop structure works for any positive n.
Print a space after each digit in both inner loops — see Example 3.
Yes. Each row prints exactly rows digits — the pivot shifts each line.
O(n²) for n rows. Each row prints O(n) digits.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

Each row combines a decreasing prefix (i..2) and an increasing suffix (1..(rows+1-i)). Row 1 prints only the suffix — 12345; row 3 gives 32123.

Next: Alternating Ascending/Descending Triangle

Continue with the alternating ascending/descending triangle in the Java number-pattern series.

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