Ascending Number Triangle in Java

Beginner
⏱️ 7 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops 1→i

What You’ll Learn

The ascending number triangle prints 1, 12, 123, 1234, 12345 — row i concatenates digits 1 through i. This tutorial covers nested-loop logic, live preview, worked Java examples, edge cases, and O(n²) complexity.

Shape Rule

Print 1..i

Row i prints digits 1, 2, …, i with no spaces.

Nested Loops

i = 1..rows

Outer loop picks row i; inner loop runs j = 1..i.

print(j)

No spaces

System.out.print(j) concatenates digits on the same row.

Foundation

Series base

Stepping stone toward Floyd’s triangle, stars, and alphabet patterns.

Live Preview

1–20 rows

Pick a row count and draw the ascending triangle instantly in the browser.

O(n²)

Complexity

Total digits ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).

Introduction

An ascending number triangle pattern grows each row by one digit: 1, 12, 123, up to 12345 for five rows. Row i always starts at 1 and runs through i.

In Java the outer loop runs i = 1..rows, the inner loop prints j from 1 up to i, then System.out.println() moves to the next line.

Why it matters?

It is a foundational nested-loop exercise — compare with Program 4 (fixed prefix descending) and continue to Program 6 (increasing suffix).

Key Highlights

Starts at 1

Every row begins at digit 1.

Growing rows

Inner loop j = 1..i lengthens each row.

vs Program 4

Program 4 keeps the first digit at rows; Program 5 grows from 1 to i.

Series Foundation

Follow Program 4; continue to Program 6 (increasing suffix) next.

In short: for each i from 1 to rows, print j from 1 up to i, then System.out.println().

📝 Problem & Approach

Given a positive integer rows (e.g. 5), print an ascending number triangle: row i prints digits 1 through i with no spaces.

Java
// rows = 5 (conceptual shape)
// 1
// 12
// 123
// 1234
// 12345

Inputs & Outputs

ItemTypeDescription
rowsintNumber of triangle lines — outer loop runs from 1 up to rows.
iintOuter loop — current row index; sets where the inner loop stops.
jintInner loop — ascending from 1 up to i.

Minimal workflow

Pseudocode
for i from 1 to rows:
    for j from 1 up to i:
        print j
    print newline

Approach comparison

ApproachIdeaBest for
Nested loops1, 12, 123, …Learning and interviews
User-input rowssc.nextInt();Flexible console programs
Spaced outputSystem.out.print(j + " ")Easier reading per row

⚡ Quick Reference

GoalPattern
Walk rowsfor (i = 1; i <= rows; i++)
Print digits 1..ifor (j = 1; j <= i; j++) System.out.print(j);
End the rowSystem.out.println();
Spaced digitsSystem.out.print(j + " ");
User inputsc.nextInt();
Program 4 contrastProgram 4 uses j = rows..i (fixed prefix); Program 5 uses j = 1..i

📋 Outer Loop vs Inner Loop vs Combined

How outer row selection and inner j=1..i work together.

Outer loop
for (i = 1; i <= rows; i++)

Picks row number i — triangle height.

Inner loop
for (j = 1; j <= i; j++)

Prints exactly i digits on row i.

Cell value
print(j)

Concatenate digits with no spaces — 123 not 1 2 3.

Learning tip
trace i=3

Dry-run row 3: j=1..3 → prints 123.

Context

When This Pattern Shows Up

Reach for this pattern when teaching nested loops, growing inner bounds, and concatenated digit output.

  1. After Program 4

    Natural follow-up after left-aligned descending triangles — now the prefix grows from 1.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with Scanner for a flexible row count.

  4. Gateway to variants

    Compare with Program 4 (descending prefix), then continue to Program 6 (increasing suffix).

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Enter a row count and draw the ascending number triangle in the browser.

Try 5, 7, or 10. Larger values still work up to 20.

Live result
Press "Draw pattern".

Examples Gallery

Three complete Java programs — fixed rows = 5, Scanner input, and a spaced-output variant. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows — inner loop prints 1..i on each line.

Example 1 — Fixed rows = 5

Hard-coded row count — inner loop prints from 1 to i.

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

When i = 3, the inner loop prints 1, 2, 3 — output 123. When i = 5, output is 12345.

📈 User Input

Read the row count with Scanner instead of hard-coding 5.

Example 2 — User Input Version

Read rows with Scanner.nextInt(); same nested loops as Example 1.

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

        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

Same nested-loop core as Example 1; only the source of rows changes.

⚡ Formatting Variant

Add spaces between digits for easier reading.

Example 3 — Spaced Output

Print a space after each digit with print(j + " ").

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

Same j = 1..i logic; only the output format adds spaces between digits.

🧠 How the Algorithm Prints Rows

1

Set up

System.out is built in; use Scanner when reading input. Set loop variables i, j with rows = 5.

Setup
2

Outer loop walks rows

for (i = 1; i <= rows; i++) — each row adds one more digit.

Row
3

Inner loop (j)

for (j = 1; j <= i; j++) — prints digits from 1 up to i.

Inner
4

New line

System.out.println() ends the row after the inner loop finishes.

Break
=

Ascending number triangle complete

Each row grows by one digit — O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — row i = 3

Trace row 3 to see how the inner loop builds 123.

jActionRow so far
1print 11
2print 212
3print 3123

After the inner loop, println() moves to the next row.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: flip j-- to j++ and watch digit order change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: continue to Program 6 for the increasing suffix pattern.

3. Console Formatting Drills

Practice System.out.print vs System.out.println() without complex math.

Example: put System.out.println() inside the inner loop by mistake.

4. Spaced Output

Add spaces between digits once the two-loop structure works.

Example: use System.out.print(j + " ") between digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for rows = 5 — total is 15 (5+4+3+2+1).

6. Input Validation Labs

Pair the pattern with Scanner return checks and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner Java courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row grows by one digit.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Match Inner Bounds

    Outer loop counts up; inner loop must run j = 1..i for the ascending prefix.

  2. 2. Prefer Scanner

    Call sc.hasNextInt() so bad input does not leave rows uninitialized.

  3. 3. Keep System.out.println() Outside

    Only call System.out.println() after the inner loop finishes the row.

  4. 4. Use print(j) for Digits

    for (j = 1; j <= i; j++) System.out.print(j) concatenates digits on one line.

  5. 5. Dry-Run rows = 3

    Trace i = 1, 2, 3 on paper before coding the full rows = 5 demo.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put System.out.println() inside the inner loop.

Common Pitfalls

Mistakes that commonly break ascending number triangle patterns.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use System.out.print(j) for digits; System.out.println() only after the inner loop.

  2. 2. Wrong Inner Bound (j <= rows)

    Every row prints rows digits — you get a rectangle, not a growing triangle.

    → Keep for (j = 1; j <= i; j++) so row length equals i.

  3. 3. Descending Inner Loop

    for (j = i; j >= 1; j--) reverses digit order — still a triangle, but not 1, 12, 123.

    → Use ascending inner loop j = 1..i for the standard pattern.

  4. 4. Inner Starts at rows

    j = rows..i produces Program 4’s fixed-prefix shape — not this pattern.

    → Start the inner loop at j = 1 for ascending rows.

  5. 5. Unchecked Scanner input

    Letters or empty input leave rows uninitialized.

    → Call sc.hasNextInt() and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single digit row

Output is just 1 on one line.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

rows = 2

Smallest triangle

Two rows: 1 and 12.

Bad input

Non-numeric Scanner input

Unchecked Scanner input leaves rows unset — call hasNextInt() first.

Large rows

Large row count

Each row prints i digits — total work grows as n(n+1)/2.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Compare Program 4

  • Program 4: fixed prefix 54321, 5432…
  • Review Program 4

2. Invert outer loop

  • Use for (i = rows; i >= 1; i--)
  • Same inner loop — tallest row first

3. Next in series

  • Continue with Program 6
  • Increasing suffix pattern 5, 45, 345…

4. Spaced output

  • Use System.out.print(j + " ") between digits
  • Same loops, wider visual spacing

Notes

  • Ascending rule. Outer loop: i = 1..rows. Inner loop: j = 1..i with print(j).
  • System.out.print stays on the line; System.out.println() advances — mix them carefully.
  • Validate rows > 0 for interactive programs; rows = 1 should print a single 1.
  • Row i prints exactly i digits — compare with Program 4 where each row prints rows - i + 1 digits.

Quick Takeaway: outer loop i = 1..rows, inner loop j = 1..i with System.out.print(j), then System.out.println().

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Spaced output (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The ascending number triangle is a compact nested-loop lesson: outer loop grows rows while the inner loop prints digits 1 through i. Master the fixed-rows version, then try user input and spaced output.

Practice the three examples above, then continue to Program 6 for the increasing suffix pattern.

Row i prints 1..i — keep System.out.print(j) for digits and System.out.println() for the row break.

💡 Best Practices

✅ Do

  • Use for (i = 1; i <= rows; i++) in the outer loop
  • Inner: for (j = 1; j <= i; j++) prints digits 1..i
  • Use System.out.print(j) for digits and System.out.println() after each row
  • Validate rows ≥ 1 for interactive programs
  • Call sc.hasNextInt() before using rows

❌ Don’t

  • Call System.out.println() inside the inner digit loop
  • Use inner bound j <= rows when triangle shape needs j <= i
  • Start inner loop at rows (that is Program 4, not this pattern)
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this ascending number triangle

Print the pattern the beginner-friendly way.

5
Core concepts
02

Outer loop

Counts up rows

Code
03

Inner loop

j = 1 to i

Code
04

Newline

Ends each row

Shape
O 05

Complexity

O(n²) time

Analysis

❓ 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 i1, 12, 123, and so on.

Continue to Program 6

Move on to 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