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.
Approach
How to Solve It
Outer loop picks the row length; inner loop prints j from 1 to i with no spaces.
Method
Idea
Best for
Fixed rows
Hard-code rows; print j
Labs and demos
Scanner input
Same loops; read rows at runtime
Interactive practice
Spaced output
print(j + " ") between digits
Easier-to-read rows
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print j
new line
Cheat sheet
Goal
Pattern
Set size
int rows = 5;
Walk rows
for (int i = 1; i <= rows; i++)
Walk digits
for (int j = 1; j <= i; j++)
Print digit
System.out.print(j);
End row
System.out.println();
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit j in a row
System.out.println
Ends the line
After the inner loop finishes
Glue digits with print, then break the line once per row.
Try it
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 resultrows = 5 · digits = 15
1
12
123
1234
12345
Trace
Worked Walkthrough — rows = 5
Trace each row: j runs from 1 to i, and digits are printed with no spaces.
i
j range
Printed row
1
1..1
1
2
1..2
12
3
1..3
123
4
1..4
1234
5
1..5
12345
Each row is the previous row plus one more digit on the right.
Code
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();
}
}
}
Output
1
12
123
1234
12345
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();
}
}
Output (when user enters 5)
Enter the number of rows: 5
1
12
123
1234
12345
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();
}
}
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
All three examples
O(n²)
O(1)
Total printed digits for n rows: 1 + 2 + … + n = n(n+1)/2.
Remember
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.