An increasing triangle from 11 prints values from the formula 9 + i + j. Row i has i numbers, each separated by a space, growing left-aligned.
Remember
Rule: for i from 1 to rows
for j from 1 to i:
print (9 + i + j) + space
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19 ← rows = 5
Nested loops plus one arithmetic formula. Follows the number-star diamond in Program 31; next is Program 33 (same shape, formula starts at 1).
Approach
How to Solve It
Start with fixed rows = 5 and base 9, then generalize both with scanf — optionally demo a smaller count for tracing.
Method
Idea
Best for
Fixed formula
Print 9 + i + j for j = 1..i
Learning, interviews, exams
Custom base
Same loops; baseVal + i + j from scanf
Shifting the whole triangle
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print (9 + i + j) and a space
print newline
Cheat sheet
Goal
Pattern
Walk each row
for (i = 1; i <= rows; i++)
Grow row length
for (j = 1; j <= i; j++)
Print value + space
printf("%d ", 9 + i + j);
End the row
printf("\n");
Custom base
printf("%d ", baseVal + i + j);
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d ", 9 + i + j)
Stays on the same line
Each value plus a separator space
printf("\n")
Ends the current line
After the inner loop
Print all values on the row without a newline, then end the row once.
Try it
Live Preview
Change the row count and the triangle updates instantly — base stays at 9 so the first value is always 11.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 values
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and list the formula results for j = 1..i.
i
Values (9 + i + j)
Printed row
1
11
11
2
12, 13
12 13
3
13, 14, 15
13 14 15
4
14, 15, 16, 17
14 15 16 17
Total values for n rows = n(n + 1)/2 (here 4 × 5 / 2 = 10). That triangular sum is why time is O(n²).
Code
C Programs
Three complete programs: fixed rows, custom base via scanf, and a small demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded row count — classic 9 + i + j formula.
C
#include <stdio.h>
int main(void)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
printf("%d ", 9 + i + j);
printf("\n");
}
return 0;
}
Output
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19
How It Works
1. Outer loop grows i.i runs from 1 to 5 — one longer row each pass.
2. Inner loop prints the formula.j runs from 1 to i; each value is 9 + i + j plus a space.
3. Newline.printf("\n") ends the row after the inner loop.
When i = 1 you get 11; when i = 3 you get 13 14 15.
Example 2 — Custom Base and Rows
Read rows and baseVal with scanf. The formula becomes baseVal + i + j.
C
#include <stdio.h>
int main(void)
{
int rows, baseVal;
int i, j;
printf("Enter rows: ");
scanf("%d", &rows);
printf("Enter base: ");
scanf("%d", &baseVal);
if (rows < 1) rows = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
printf("%d ", baseVal + i + j);
printf("\n");
}
return 0;
}
Output (when user enters rows 3, base 9)
Enter rows: 3
Enter base: 9
11
12 13
13 14 15
How It Works
1. Prompt for both inputs.rows sets height; baseVal replaces the hard-coded 9.
2. Same nested core. Only the formula offset changes — first value is always baseVal + 2.
3. Safer input tip. Unchecked scanf leaves variables uninitialized on bad input. Prefer:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Enter a positive whole number of rows.\n");
return 1;
}
if (scanf("%d", &baseVal) != 1)
{
printf("Enter a whole number base.\n");
return 1;
}
Example 3 — Compact rows = 3
Same formula with a smaller row count for quick paper tracing.
C
#include <stdio.h>
int main(void)
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
printf("%d ", 9 + i + j);
printf("\n");
}
return 0;
}
Output
11
12 13
13 14 15
How It Works
1. Same structure. Only rows changes from 5 to 3 — the formula stays 9 + i + j.
2. Trace on paper. For i = 2: 9+2+1 = 12, 9+2+2 = 13 → 12 13.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or a custom base.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong formula
Starts at the wrong number
Using i + j or i + j - 1 changes the first value. Keep 9 + i + j for a start at 11 (or swap in baseVal).
no space
Glued numbers
printf("%d", 9 + i + j) without a space merges values like 1213. Always append a space in the format string.
\n inside
Broken rows
If printf("\n") sits inside the inner loop, each value lands on its own line. Call the newline only after the row finishes.
j to rows
Rectangle instead of triangle
Looping j to rows instead of i prints the same count every line. Inner bound must be i.
rows = 1
Smallest triangle
Output is a single 11 — a good sanity check.
scanf
Check both return values
Example 2 reads two integers. Test each scanf(...) == 1 so neither variable stays uninitialized.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(rows²)
O(1)
Small demo (Example 3)
O(rows²)
O(1)
Total values = n(n + 1)/2 — quadratic in n. For rows = 5 that is 15 printed numbers.
Remember
Key Takeaways
Rule: for each i, print 9 + i + j for j = 1..i.
Base offset:9 makes the first value 11; swap in baseVal to shift the triangle.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time from n(n + 1)/2 values; O(1) extra space.
One line: for i from 1 to rows, print 9 + i + j for j = 1..i, then printf("\n").
Frequently Asked Questions
Because the printed value is 9 + i + j. On the first row i = 1 and j = 1, so 9 + 1 + 1 = 11.
It is a base offset. Change 9 to any base value to shift the entire triangle — see Example 2.
Program 32 uses 9 + i + j (starts at 11). Program 33 uses i + j - 1 (starts at 1).
printf("%d ", 9 + i + j) keeps values separated on the same row. printf("\n") ends the row.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Check scanf's return value and reject non-positive row counts so the outer loop has a valid range.
Yes — printf("%d ", baseVal + i + j) lets the user pick any starting offset — see Example 2.
🤔
Did you know?
Each printed value is computed as 9 + i + j. Row i = 1 prints 11; row i = 2 prints 12 and 13 — a left-shifted increasing triangle.