An ascending number triangle starts with one digit on row 1 and grows by one digit each row: row i prints 1 through i with no spaces between digits.
Remember
Rule: for i from 1 to rows
for j from 1 to i: print j (no space)
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 counts up from 1 to rows. Inner loop prints digits 1..i. End each row with printf("\n").
Method
Idea
Best for
Growing inner bound
Outer i = 1..rows; inner j = 1..i; print j
Learning, interviews, exams
Spaced digits
printf("%d ", j) instead of printf("%d", j)
Easier reading on each row
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print j
print newline
Cheat sheet
Goal
Pattern
Pick each row
for (i = 1; i <= rows; i++)
Print digits
for (j = 1; j <= i; j++) printf("%d", j);
End of row
printf("\n");
Spaced form
printf("%d ", j);
Shrinking version
for (i = rows; i >= 1; i--) (Program 1)
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j)
Stays on the same line
Each digit on the row
printf("\n")
Ends the current line
After the inner loop
Print all digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the ascending triangle updates instantly — capped at 9 so every digit stays a single character.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
1
12
123
1234
12345
Trace
Worked Walkthrough — Row i = 4
Trace the inner loop when the outer index is 4 — four digits with no spaces.
j
Prints
Line so far
1
1
1
2
2
12
3
3
123
4
4
1234
Full row: 1234. For n rows, total digits = n(n+1)/2 → O(n²).
Code
C Programs
Three complete programs: fixed rows, scanf input, and a spaced-digit variant. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — digits printed tight with printf("%d", j).
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
Output
1
12
123
1234
12345
How It Works
1. Outer loop.i runs from 1 to 5 — each pass is one row.
2. Inner loop.j runs from 1 to i, so row i has exactly i digits.
3. Tight print.printf("%d", j) keeps digits on one line; printf("\n") starts the next row.
Example 2 — User Input Rows
Read rows with scanf and reject non-positive values.
C
#include <stdio.h>
int main(void)
{
int rows;
int i, j;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1
12
123
1234
How It Works
1. Prompt and validate. Reject bad input with a clear message.
2. Same core. Only the outer loop limit comes from the user — digit logic stays identical.
3. Safer input tip. Cap demos to single-digit rows:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 9)
{
printf("Enter a whole number from 1 to 9.\n");
return 1;
}
Example 3 — Spaced Digits
Same loops with printf("%d ", j) so digits are easier to read.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
printf("%d ", j);
printf("\n");
}
return 0;
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
How It Works
1. Same structure. Only the format string changes — loops stay identical to Example 1.
2. Space after each digit.printf("%d ", j) separates values for clearer scanning.
3. When to use it. Prefer the tight form for classic screenshots; use spaced form when teaching or tracing by hand.
Edge Cases & Pitfalls
Check these before calling the solution done.
i--
Shrinking instead of growing
Counting i down from rows prints the wide row first (Program 1). Use for (i = 1; i <= rows; i++) to grow.
\n inside
Broken rows
If printf("\n") sits inside the inner loop, each digit lands on its own line. Call the newline only after the inner loop.
j <= rows
Rectangle instead of triangle
If the inner loop uses j <= rows every row, every line prints 1..rows. Keep j <= i.
rows = 1
Single row
Output is just 1 — a good sanity check for input validation.
rows > 9
Multi-digit values
Past 9, values like 10 take two characters and break the tight look. Cap demos at 9 or switch to spaced / fixed-width format.
scanf
Check the return value
If scanf fails, rows may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Spaced digits (Example 3)
O(n²)
O(1)
Total digits = 1 + 2 + … + n = n(n+1)/2. For n = 5 that is 15 prints.
Remember
Key Takeaways
Rule: on row i, print digits 1 through i.
Grow upward: outer loop counts 1..rows so each row adds one digit.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time from n(n+1)/2 digits; O(1) extra space.
One line: for i = 1..rows, print j for j = 1..i, then printf("\n").
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.
When i = 1, the inner loop runs j from 1 to 1 — exactly one digit.
printf("%d", j) stays on the same line. printf("\n") ends the current row after the inner loop.
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.
Replace 5 with rows in the outer loop bound — see Example 2.
Count the outer loop down: for (i = rows; i >= 1; i--). Keep the inner loop as for (j = 1; j <= i; j++) — that is Program 1.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/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 — still O(n²) total prints.