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

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.
Print 1..i
Row i prints digits 1, 2, …, i with no spaces.
i = 1..rows
Outer loop picks row i; inner loop runs j = 1..i.
No spaces
System.out.print(j) concatenates digits on the same row.
Series base
Stepping stone toward Floyd’s triangle, stars, and alphabet patterns.
1–20 rows
Pick a row count and draw the ascending triangle instantly in the browser.
Complexity
Total digits ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).
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.
It is a foundational nested-loop exercise — compare with Program 4 (fixed prefix descending) and continue to Program 6 (increasing suffix).
Every row begins at digit 1.
Inner loop j = 1..i lengthens each row.
Program 4 keeps the first digit at rows; Program 5 grows from 1 to i.
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().
Given a positive integer rows (e.g. 5), print an ascending number triangle: row i prints digits 1 through i with no spaces.
// rows = 5 (conceptual shape)
// 1
// 12
// 123
// 1234
// 12345 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines — outer loop runs from 1 up to rows. |
i | int | Outer loop — current row index; sets where the inner loop stops. |
j | int | Inner loop — ascending from 1 up to i. |
for i from 1 to rows:
for j from 1 up to i:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 1, 12, 123, … | Learning and interviews |
| User-input rows | sc.nextInt(); | Flexible console programs |
| Spaced output | System.out.print(j + " ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= rows; i++) |
| Print digits 1..i | for (j = 1; j <= i; j++) System.out.print(j); |
| End the row | System.out.println(); |
| Spaced digits | System.out.print(j + " "); |
| User input | sc.nextInt(); |
| Program 4 contrast | Program 4 uses j = rows..i (fixed prefix); Program 5 uses j = 1..i |
How outer row selection and inner j=1..i work together.
for (i = 1; i <= rows; i++)Picks row number i — triangle height.
for (j = 1; j <= i; j++)Prints exactly i digits on row i.
print(j)Concatenate digits with no spaces — 123 not 1 2 3.
trace i=3Dry-run row 3: j=1..3 → prints 123.
Reach for this pattern when teaching nested loops, growing inner bounds, and concatenated digit output.
Natural follow-up after left-aligned descending triangles — now the prefix grows from 1.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 4 (descending prefix), then continue to Program 6 (increasing suffix).
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.
Enter a row count and draw the ascending number triangle in the browser.
Three complete Java programs — fixed rows = 5, Scanner input, and a spaced-output variant. Click View Output to reveal sample console results.
Print five rows — inner loop prints 1..i on each line.
rows = 5Hard-coded row count — inner loop prints from 1 to i.
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();
}
}
} When i = 3, the inner loop prints 1, 2, 3 — output 123. When i = 5, output is 12345.
Read the row count with Scanner instead of hard-coding 5.
Read rows with Scanner.nextInt(); same nested loops as Example 1.
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();
}
} Same nested-loop core as Example 1; only the source of rows changes.
Add spaces between digits for easier reading.
Print a space after each digit with print(j + " ").
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();
}
}
} Same j = 1..i logic; only the output format adds spaces between digits.
System.out is built in; use Scanner when reading input. Set loop variables i, j with rows = 5.
for (i = 1; i <= rows; i++) — each row adds one more digit.
for (j = 1; j <= i; j++) — prints digits from 1 up to i.
System.out.println() ends the row after the inner loop finishes.
Each row grows by one digit — O(n²) time, O(1) extra memory.
i = 3Trace row 3 to see how the inner loop builds 123.
| j | Action | Row so far |
|---|---|---|
| 1 | print 1 | 1 |
| 2 | print 2 | 12 |
| 3 | print 3 | 123 |
After the inner loop, println() moves to the next row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: flip j-- to j++ and watch digit order change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 6 for the increasing suffix pattern.
Practice System.out.print vs System.out.println() without complex math.
Example: put System.out.println() inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use System.out.print(j + " ") between digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for rows = 5 — total is 15 (5+4+3+2+1).
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.
Why this pattern earns a permanent spot in beginner Java courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
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.
Small habits that keep number-pattern code clean.
Outer loop counts up; inner loop must run j = 1..i for the ascending prefix.
ScannerCall sc.hasNextInt() so bad input does not leave rows uninitialized.
Only call System.out.println() after the inner loop finishes the row.
for (j = 1; j <= i; j++) System.out.print(j) concatenates digits on one line.
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.
Mistakes that commonly break ascending number triangle patterns.
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.
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.
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.
j = rows..i produces Program 4’s fixed-prefix shape — not this pattern.
→ Start the inner loop at j = 1 for ascending rows.
Letters or empty input leave rows uninitialized.
→ Call sc.hasNextInt() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 1 and 12.
Unchecked Scanner input leaves rows unset — call hasNextInt() first.
Each row prints i digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
54321, 5432…for (i = rows; i >= 1; i--)5, 45, 345…System.out.print(j + " ") between digitsi = 1..rows. Inner loop: j = 1..i with print(j).System.out.print stays on the line; System.out.println() advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.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().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Spaced output (Example 3) | O(n²) | O(1) |
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.
for (i = 1; i <= rows; i++) in the outer loopfor (j = 1; j <= i; j++) prints digits 1..iSystem.out.print(j) for digits and System.out.println() after each rowrows ≥ 1 for interactive programssc.hasNextInt() before using rowsSystem.out.println() inside the inner digit loopj <= rows when triangle shape needs j <= irows (that is Program 4, not this pattern)rows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints 1..i
DefinitionCounts up rows
Codej = 1 to i
CodeEnds each row
ShapeO(n²) time
AnalysisThe inner loop always runs from j = 1 to j = i, so row i prints digits 1 through i — 1, 12, 123, and so on.
Move on to the increasing suffix number pattern in the Java number-pattern series.
12 people found this page helpful