Java Sequential Number Triangle Pattern (Narrowing)

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

What Is This Pattern?

A sequential decreasing-width triangle prints consecutive integers while each row gets one value shorter — from rows numbers on the first line down to a single value.

Remember
Rule: k = 1; for i from 1 to rows,
      print (rows - i + 1) values with format("%3d", k++)

  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15     ← rows = 5

Unlike Program 37 (palindrome per row), here one shared counter k continues across the whole triangle.

How to Solve It

Walk i from 1 to rows; on each row print rows − i + 1 consecutive values via k++.

MethodIdeaBest for
Shared counter + formatk++ with %3d; inner loop shrinks each rowLearning, interviews, exams
Custom startSame loops; initialize k to any start valueWhen the sequence should not begin at 1

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from rows down to i:
        print k (width 3); k = k + 1
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Shrinking widthfor (int j = rows; j >= i; j--)
Print next valueSystem.out.format("%3d", k++);
End the rowSystem.out.println();
Values per rowrows - i + 1

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.format / printfStays on the same lineEach formatted number
System.out.printlnEnds the current lineAfter the inner loop finishes

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

Live Preview

Change the row count and the sequential triangle 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 · 15 values
  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15

Worked Walkthrough — rows = 5

Trace the count per row (rows − i + 1) and the values taken from k.

iCountValues from kPrinted row
151 … 51 2 3 4 5
246 … 96 7 8 9
3310 … 1210 11 12
4213, 1413 14
511515

Total values = 5 + 4 + 3 + 2 + 1 = 15 — the triangular number n(n+1)/2.

Java Programs

Three complete programs: fixed rows = 5, Scanner input, and a custom start for k. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded size — shared k with %3d formatting.

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

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= i; j--)
                System.out.format("%3d", k++);
            System.out.println();
        }
    }
}

How It Works

1. Counter outside. k starts at 1 before the outer loop and never resets.

2. Inner loop shrinks. j runs from rows down to i — fewer prints each row.

3. Formatted print. format("%3d", k++) prints the next value in a 3-column field.

When i = 1: five values 1…5. When i = 5: a single 15.

Example 2 — Rows Input

Read rows at runtime. Same counter and shrinking inner loop.

Java
import java.util.Scanner;

public class SequentialNumberTriangleInput {
    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;

        int k = 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= i; j--)
                System.out.format("%3d", 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 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 — Custom Start k = 10

Same shrinking loops — only the initial counter value changes.

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

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= i; j--)
                System.out.format("%3d", k++);
            System.out.println();
        }
    }
}

How It Works

1. Same structure. Shrinking inner loop and %3d — only k starts at 10.

2. Fifteen values still. Sequence runs from 10 through 24.

3. Wider fields tip. Use %4d when totals grow past two digits.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Column of numbers

If println is inside the inner loop, each value lands on its own line. Use format for values; println only after the inner loop.

Reset k

Restarted sequence

Declaring k = 1 inside the outer loop restarts the count every row instead of continuing.

Wrong inner bound

Rectangle

Using j <= rows on every row prints a rectangle instead of a shrinking triangle.

No %3d

Crowded digits

Plain print(k++) makes two-digit values crowd earlier columns.

rows = 1

Single value

Output is just 1 on one line.

Bad input

Use hasNextInt

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

Time and Space Complexity

ProgramTimeExtra space
Sequential shrinking (Examples 1–3)O(n²)O(1)

Total values = n(n+1)/2 — still quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: row i prints rows − i + 1 consecutive values via k++.
  • Keep k outside: one shared counter continues the sequence across rows.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row, print rows − i + 1 values with format("%3d", k++), then println().

Frequently Asked Questions

Because the row lengths are 5, 4, 3, 2, and 1. Their sum is 5+4+3+2+1 = 15, which equals n(n+1)/2.
%3d prints an integer right-aligned in a field width of 3 characters, which keeps columns aligned when numbers reach two digits.
Each row prints a different count of numbers, but the sequence must continue globally (1, 2, 3, …). k stores the next value and increments after every print.
Yes. Initialize k with your starting value instead of 1 — see Example 3.
Program 37 builds a palindrome on each row. Program 38 prints one continuous ascending sequence with shrinking row widths.
O(n²) where n is the number of rows. Total printed values equal 1+2+…+n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Row i prints rows - i + 1 consecutive numbers via a shared k counter. Total values for n rows is the triangular number n(n+1)/2 — 15 when rows = 5.

Next: Rotating Numbers Pattern

Move on to the rotating numbers pattern in the Java number-pattern series.

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