An ascending number triangle grows one digit per row: row i prints digits 1 through i.
Remember
Rule: on row i, print j for j = 1..i
1
12
123
1234
12345 ← rows = 5
Follows the left-aligned descending triangle in Program 4; next is the reverse ascending triangle in Program 6.
Approach
How to Solve It
Outer loop picks row i from 1 to rows. Inner loop prints digits 1..i.
Method
Idea
Best for
Tight digits
print(j, end="") with no spaces
Learning, interviews, exams
Spaced digits
print(j, end=" ")
Easier reading / wider values
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Walk rows
for i in range(1, rows + 1):
Walk digits
for j in range(1, i + 1):
Print digit
print(j, end="")
Spaced digit
print(j, end=" ")
End the row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(j, end="")
Stays on the same line
Each digit
print()
Ends the current line
After the inner loop finishes a row
Print digits without a newline, then end the row once. Putting bare print() inside the inner loop turns the triangle into a vertical list.
Try it
Live Preview
Change the row count and the ascending triangle updates instantly.
Whole numbers from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
1
12
123
1234
12345
Trace
Worked Walkthrough — row i = 4
Trace four digits glued together as 1234.
j
Print
Line so far
1
1
1
2
2
12
3
3
123
4
4
1234
After j finishes, bare print() starts the next row. Row 5 will print one more digit — 12345.
Code
Python Programs
Three complete programs: fixed rows = 5, input() rows, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — outer loop grows the row, inner loop prints 1..i.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
print()
Output
1
12
123
1234
12345
How It Works
1. Outer loop grows.i runs from 1 to 5 — each value is one row.
2. Inner loop prints 1..i.j runs from 1 to i; each digit uses print(j, end="").
3. Newline. Bare print() runs only after the inner loop finishes.
Example 2 — User Input Rows
Read the row count with input() and build the same triangle.
Python
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Please enter a positive integer.")
raise SystemExit(1)
if rows < 1:
print("Please enter a positive integer.")
raise SystemExit(1)
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
print()
2. Same triangle. Only the bound changes from the literal 5 to the user value.
3. Safer input tip. Cap demos for readable output:
Safer input tip
if rows < 1 or rows > 9:
print("Enter a whole number from 1 to 9.")
raise SystemExit(1)
Example 3 — Spaced Digits
Same loops — add a space after each digit for easier reading.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
How It Works
1. Only the print changes.print(j, end=" ") instead of print(j, end="").
2. Same bounds. Outer and inner limits match Example 1 exactly.
3. Useful for wider values. Spaces keep multi-digit numbers readable when rows grows.
Edge Cases & Pitfalls
Check these before calling the solution done.
print() inside
print() inside the inner loop
That prints one digit per line. Call bare print() only after the inner loop.
no end
Forgot end=""
Default print(j) puts each digit on its own line. Use end="" (or end=" ") inside the row.
no newline
Missing bare print()
Without it, every digit glues onto one endless line.
outer down
Outer loop counts down
You get Program 1’s shrinking triangle (12345, 1234, …). Keep i from 1 up to rows.
rows = 1
Single row
Output is just 1. A good sanity check for input validation.
input()
Catch ValueError
Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
Total prints are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n.
Remember
Key Takeaways
Rule: on row i, print j for j = 1..i.
Grow: outer loop counts up so each row adds one digit.
Break the row: call bare print() only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for each row i from 1 to rows, print digits 1..i, then end the line.
Frequently Asked Questions
The outer loop runs i from 1 to rows. For each row i, the inner loop runs j from 1 to i and prints j. Row 1 prints 1, row 2 prints 12, and so on until row rows prints 1..rows.
Because the outer loop counts up and the inner bound equals i. When i increases, each row prints one more digit than the previous row.
Program 4 prints rows..i in descending order (54321, 5432, …). Program 5 prints 1..i in ascending order — a growing triangle instead of a shrinking one.
Program 1 counts the outer loop down and prints 12345, 1234, … Program 5 counts up and prints 1, 12, 123, … — same inner loop, opposite outer direction.
Printing a digit with end="" stays on the same line. Bare print() ends the current row. Digits use end=""; the row break uses print() after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
Use print(j, end=" ") instead of print(j, end="") — see Example 3.
Use try/except ValueError around int(input()) and require rows >= 1 — see Example 2.
🤔
Did you know?
Row i prints digits 1 through i. The outer loop counts up from 1 to rows, so each row grows by one digit — total prints = n(n+1)/2, time O(n²).