Java Ascending Number Triangle Pattern

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

What Is This Pattern?

An ascending number triangle grows one digit per row: 1, 12, 123, up to 12345 for five rows. Row i always prints 1 through i.

Remember
Rule: for i = 1..rows
        for j = 1..i: print j

1
12
123
1234
12345     ← rows = 5

Unlike Program 4 (descending prefixes like 54321), here every row starts at 1 and climbs upward.

How to Solve It

Outer loop picks the row length; inner loop prints j from 1 to i with no spaces.

MethodIdeaBest for
Fixed rowsHard-code rows; print jLabs and demos
Scanner inputSame loops; read rows at runtimeInteractive practice
Spaced outputprint(j + " ") between digitsEasier-to-read rows

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print j
    new line

Cheat sheet

GoalPattern
Set sizeint rows = 5;
Walk rowsfor (int i = 1; i <= rows; i++)
Walk digitsfor (int j = 1; j <= i; j++)
Print digitSystem.out.print(j);
End rowSystem.out.println();

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit j in a row
System.out.printlnEnds the lineAfter the inner loop finishes

Glue digits with print, then break the line once per row.

Live Preview

Change the row count and the ascending 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 · digits = 15
1
12
123
1234
12345

Worked Walkthrough — rows = 5

Trace each row: j runs from 1 to i, and digits are printed with no spaces.

ij rangePrinted row
11..11
21..212
31..3123
41..41234
51..512345

Each row is the previous row plus one more digit on the right.

Java Programs

Three complete programs: fixed rows = 5, Scanner input, and spaced output. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded size — outer loop for rows, inner loop prints j with no spaces.

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

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

How It Works

1. Outer loop. i is the row number and the last digit on that row.

2. Inner loop. j runs from 1 to i, so row i has exactly i digits.

3. Print and break. print(j) concatenates digits; println() starts the next line.

Example 2 — Scanner Input

Read rows at runtime. Same nested-loop logic.

Java
import java.util.Scanner;

public class AscendingNumberTriangleInput {
    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 = 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 loops. Only the source of rows changes — the j = 1..i logic 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 Output

Same j = 1..i logic — a space after each digit for clearer rows.

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

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

How It Works

1. Same range. Still print j from 1 to i.

2. Different format. print(j + " ") separates digits so multi-digit rows stay readable.

3. Same shape. The triangle still grows by one value per row — only spacing changes.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Vertical list

Putting println() inside the inner loop prints one digit per line instead of concatenating a row.

Wrong start

Looks like Program 4

Starting j at rows and counting down produces descending prefixes — not this ascending triangle.

rows = 1

Single digit

Output is just 1.

rows = 0

Empty output

The outer loop never runs. Guard interactive input with rows >= 1.

print(i)

Repeated digits

Printing i instead of j gives 1, 22, 333 — a different pattern.

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)

Total printed digits for n rows: 1 + 2 + … + n = n(n+1)/2.

Key Takeaways

  • Rule: outer i=1..rows, inner j=1..i, print j.
  • Shape: each row starts at 1 and grows by one digit.
  • Spacing: use print for digits and one println per row.
  • Complexity: O(n²) prints for n rows.

One line: for each row i, print 1..i with print(j), then println().

Frequently Asked Questions

Because the inner loop always begins at j = 1. The outer loop sets how far the row extends with j <= i.
The outer loop runs i from 1 to rows. For each i, the inner loop runs j from 1 to i and prints j, then println ends the row.
Row i runs the inner loop i times (j = 1..i), so each row has one more digit than the row above.
Yes. Print System.out.print(j + " ") in the inner loop — see Example 3.
Reverse the outer loop: for (int i = rows; i >= 1; i--) and keep the inner loop as j = 1..i.
O(n²) for n rows. Total printed digits are 1+2+...+n = n(n+1)/2.
Program 4 prints 54321, 5432, 543 (descending from rows). Program 5 prints 1, 12, 123 (ascending from 1).
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

The inner loop always runs from j = 1 to j = i, so row i prints digits 1 through i — 1, 12, 123, and so on.

Next: Increasing Suffix Pattern

Continue with the increasing suffix number pattern in the Java number-pattern series.

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