An alternating odd/even number triangle grows each row by one number while switching sequences: odd rows print odds from 1, even rows print evens from 2.
Remember
Rule: odd row → k=1; even row → k=2; print i values with k += 2
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9 ← 5 rows
In C you pick start value k with i % 2, print k with a trailing space, update k += 2, then printf("\n") ends the row. Program 17 is odd-only left-shifted; this one alternates parity by row.
Approach
How to Solve It
Master row-parity start values first, then optionally flip which rows get odds vs evens.
Method
Idea
Best for
Parity + k += 2
Odd rows start at 1; even rows at 2; step by 2
Learning, interviews, exams
Flipped parity
Swap the starts so even rows print odds
Quick variant once the base clicks
Pseudocode
Pseudocode
for i from 1 to rows:
if i is even: k = 2 else k = 1
repeat i times:
print k and a space
k = k + 2
print newline
Cheat sheet
Goal
Pattern
Grow each row
for (i = 1; i <= rows; i++)
Pick start k
if (i % 2 == 0) k = 2; else k = 1;
Print + step
printf("%d ", k); k += 2;
End the row
printf("\n");
Ternary shortcut
int k = (i % 2 == 0) ? 2 : 1;
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d ", k)
Stays on the same line
Each number k plus a space
printf("\n")
Ends the current line
After the inner loop
Print all numbers without a newline, then end the row once.
Try it
Live Preview
Change the row count and the odd/even triangle updates instantly — including the total number count.
Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 numbers
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9
Trace
Worked Walkthrough — rows = 4
Trace each row’s parity, start value k, and the numbers printed.
i
Parity
Start k
Printed row
1
odd
1
1
2
even
2
2 4
3
odd
1
1 3 5
4
even
2
2 4 6 8
Total numbers are 1+2+3+4 = 10 — the triangular number n(n+1)/2.
Code
C Programs
Three complete programs: fixed height, input with a ternary, and flipped row parity. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop grows the length; i % 2 picks start k; inner loop prints with k += 2.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++)
{
if (i % 2 == 0)
k = 2;
else
k = 1;
for (j = 1; j <= i; j++)
{
printf("%d ", k);
k += 2;
}
printf("\n");
}
return 0;
}
Output
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9
How It Works
1. Grow.for (i = 1; i <= rows; i++) makes each row print i numbers.
2. Set k. Odd i starts at 1; even i starts at 2 — then k += 2 keeps the row odd-only or even-only.
3. Newline.printf("\n") after the inner loop starts the next longer row.
Example 2 — User Input with Ternary
Read the row count with scanf; use a compact ternary for k.
C
#include <stdio.h>
int main(void)
{
int rows;
int i, j, k;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Please enter a positive whole number.\n");
return 1;
}
for (i = 1; i <= rows; i++)
{
k = (i % 2 == 0) ? 2 : 1;
for (j = 1; j <= i; j++)
{
printf("%d ", k);
k += 2;
}
printf("\n");
}
return 0;
}
Output (when user enters 4)
1
2 4
1 3 5
2 4 6 8
How It Works
1. Validate rows. Check scanf; require rows >= 1.
2. Same logic. The ternary (i % 2 == 0) ? 2 : 1 replaces the if/else — four rows end at 2 4 6 8.
Example 3 — Flipped Row Parity
Even rows start at 1 (odds); odd rows start at 2 (evens).
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++)
{
if (i % 2 == 0)
k = 1;
else
k = 2;
for (j = 1; j <= i; j++)
{
printf("%d ", k);
k += 2;
}
printf("\n");
}
return 0;
}
Output
2
1 3
2 4 6
1 3 5 7
2 4 6 8 10
How It Works
1. Swap starts. Even rows get k = 1; odd rows get k = 2.
2. Same step.k += 2 still keeps each row odd-only or even-only — only the assignment flipped.
Edge Cases & Pitfalls
Check these before calling the solution done.
stale k
Not resetting k each row
Reusing k from the previous row mixes odd and even sequences. Set k at the start of every outer iteration.
wrong step
k++ instead of k += 2
That mixes odd and even within the same row. Keep stepping by 2.
newline early
printf("%d\n", k) inside the inner loop
That puts each number on its own line. Use printf("%d ", k); call printf("\n") only after the row finishes.
rows = 1
Single number
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
There are n rows printing 1+2+…+n numbers, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: odd rows start at 1; even rows at 2; print i values with k += 2.
Reset k: set the start value at the beginning of every row — do not carry it over.
printf vs newline: numbers stay on the line with a space; printf("\n") advances after each row.
Next step: Program 19 fills a triangle with the digit 5.
One line: for each row, set k from parity, print i values stepping by 2 with printf, then printf("\n").
Frequently Asked Questions
We check i % 2. If i is odd, set k = 1 for odd numbers; if i is even, set k = 2 for even numbers.
Row 2 is even, so k starts at 2. The inner loop prints k then adds 2 twice: 2, then 4.
printf("%d ", k) stays on the same line. printf("\n") ends the current line. Numbers use printf with a trailing space; the row break uses printf("\n") after the inner loop.
Row 3 is odd, so k starts at 1 and increments by 2 three times: 1, 3, 5.
After printing k, update k += 2 so the sequence stays odd or even while increasing.
Yes. Change the odd-row start from 1 to 3 and keep k += 2 to stay in the odd sequence.
Yes. Swap the condition so even rows start at 1 and odd rows start at 2.
O(n²) for n rows. Total printf calls equal n+(n-1)+…+1 = n(n+1)/2.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows 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?
Row parity picks the start value: odd rows begin at 1, even rows at 2. Then k += 2 keeps each row odd-only or even-only — still O(n²) total prints for n rows.