A left-shifted odd number triangle prints consecutive odd numbers on each row while the starting odd value increases by 2 — so each row is shorter and shifts left.
Remember
Rule: for i = 1..max step 2, print j = i..max step 2
13579
3579
579
79
9 ← max = 9 (or 10 with odd-only loops)
In C you solve it with nested loops that both step by 2: for (i = 1; i <= max; i += 2) and for (j = i; j <= max; j += 2), then printf("\n") ends the row. Start at 2 for an even-number mirror.
Approach
How to Solve It
Master the odd-only += 2 loops first, then optionally mirror with even starts.
Method
Idea
Best for
Odd += 2
Outer picks start odd; inner prints odds up to max
Learning step size, this pattern
Even mirror
Same structure starting at 2
Contrast / even-only variant
Pseudocode
Pseudocode
for i from 1 to max step 2:
for j from i to max step 2:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Pick row start (odd)
for (i = 1; i <= max; i += 2)
Print odds to max
for (j = i; j <= max; j += 2) printf("%d", j);
End the row
printf("\n");
Force odd max
if (max % 2 == 0) max -= 1;
Even mirror
for (i = 2; i <= max; i += 2) with the same inner shape
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j)
Stays on the same line
Each odd number j
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 maximum and the odd triangle updates instantly. Even values are adjusted down to the nearest odd number.
Whole numbers from 1 to 15. Even input becomes the next lower odd max (e.g. 10 → 9).
Live resultmax 9 · 15 numbers
13579
3579
579
79
9
Trace
Worked Walkthrough — max = 9
Trace each outer-loop value of i and the odd j values printed on that row.
i
Inner j
Printed row
Count
1
1, 3, 5, 7, 9
13579
5
3
3, 5, 7, 9
3579
4
5
5, 7, 9
579
3
7
7, 9
79
2
9
9
9
1
Total prints are 5+4+3+2+1 = 15 — triangular in the count of odd values up to max.
Code
C Programs
Three complete programs: fixed max, input with odd forcing, and an even-number mirror. Use View Output for sample results.
Example 1 — Fixed max = 10
Both loops step by 2 from odd starts, so only odds print (up through 9).
C
#include <stdio.h>
int main(void)
{
int i, j;
for (i = 1; i <= 10; i += 2)
{
for (j = i; j <= 10; j += 2)
printf("%d", j);
printf("\n");
}
return 0;
}
Output
13579
3579
579
79
9
How It Works
1. Odd starts.for (i = 1; i <= 10; i += 2) yields 1, 3, 5, 7, 9 — the first number on each row.
2. Odds to max. When i = 1, the inner loop prints 13579; when i = 9, just 9.
3. Newline.printf("\n") after the inner loop starts the next shorter left-shifted row.
Example 2 — User Input
Read the maximum with scanf; force it odd if the user enters an even number.
C
#include <stdio.h>
int main(void)
{
int max;
int i, j;
printf("Enter the maximum value: ");
if (scanf("%d", &max) != 1 || max < 1)
{
printf("Please enter a positive whole number.\n");
return 1;
}
if (max % 2 == 0)
max -= 1;
for (i = 1; i <= max; i += 2)
{
for (j = i; j <= max; j += 2)
printf("%d", j);
printf("\n");
}
return 0;
}
Output (when user enters 8)
1357
357
57
7
How It Works
1. Validate and force odd. Check scanf; if (max % 2 == 0) max -= 1 turns 8 into 7.
2. Same nested loops. With max = 7, rows end at 7 instead of 9.
Example 3 — Even Number Mirror
Same left-shift structure, but both loops start at 2 and visit only even values.
C
#include <stdio.h>
int main(void)
{
int max = 10;
int i, j;
for (i = 2; i <= max; i += 2)
{
for (j = i; j <= max; j += 2)
printf("%d", j);
printf("\n");
}
return 0;
}
Output
246810
46810
6810
810
10
How It Works
1. Even starts.i = 2 and j += 2 visit 2, 4, 6, 8, 10 only.
2. Same left shift. Each row starts later, so the triangle shortens — compare with Example 1 to see how the start value changes the sequence.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong step
j++ instead of j += 2
That includes even numbers. Keep stepping by 2 for odd-only rows.
even max
Even maximum without adjustment
Odd-only loops still skip 10, but forcing max -= 1 makes the bound explicit when reading input.
newline early
printf("%d\n", j) inside the inner loop
That puts each number on its own line. Use printf("%d", j); call printf("\n") only after the row finishes.
max = 1
Single odd
Output is just 1 — a good sanity check.
max ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
multi-digit
printf("%d", 10) concatenates
The even mirror prints 246810 with no separators — fine for labs; use printf("%d ", j) if you want spaced columns.
Analysis
Time and Space Complexity
Program
Time
Extra space
Odd loops (Examples 1–2)
O(n²)
O(1)
Even mirror (Example 3)
O(n²)
O(1)
About k = (n+1)/2 odd values produce k(k+1)/2 prints — still quadratic in the maximum n.
Remember
Key Takeaways
Rule: for odd i from 1 to max, print odd j from i to max.
Step by 2:+= 2 on both loops is what keeps the output odd-only.
printf vs newline: numbers stay on the line; printf("\n") advances after each row.
Next step: Program 18 alternates odd and even rows.
One line: for each odd start from 1 to max, print odds from i to max with printf, then printf("\n").
Frequently Asked Questions
Both loops increment by 2 (i += 2 and j += 2), so they visit only odd values: 1, 3, 5, 7, 9.
Each next row starts from a larger odd number (i increases by 2), so fewer digits print on that row.
On the last iteration, i = 9, so the inner loop prints only 9 once.
printf("%d", j) stays on the same line. printf("\n") ends the current line. Digits use printf; the row break uses printf("\n") after the inner loop.
Not with += 2 starting from odd numbers — that visits only odd values. To include 10, use j++ or handle even values separately.
Subtract 1 (max -= 1) so the last printed odd matches the intended width, as shown in Example 2.
O(n²) for maximum value n. Only about half the numbers are visited due to step size 2, but nested loops still dominate.
Check scanf's return value: if (scanf("%d", &max) != 1) handle bad input. Unchecked scanf leaves max uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Both loops step by 2 with i += 2 and j += 2, so only odd numbers print. Each row starts at a larger odd value, so the triangle shifts left — still O(n²) for maximum n.