Shape Rule
i % 2 picks parity
Row 1 prints 1, row 2 prints 2 4, row 3 prints 1 3 5, and so on as width grows.

The alternating odd/even number triangle switches row parity to print odd or even sequences — a natural step after left-shifted odd patterns. This tutorial covers the shape rule, loop structure, a live preview, algorithm steps, worked Java examples, edge cases, and complexity.
i % 2 picks parity
Row 1 prints 1, row 2 prints 2 4, row 3 prints 1 3 5, and so on as width grows.
1..rows
for (i = 1; i <= rows; i++) makes each new row one number longer than the previous.
k += 2 sequence
for (j = 1; j <= i; j++) prints k, then k += 2 keeps odd or even parity on each row.
Same line / next line
Numbers use System.out.print(k + " "); end each row with System.out.println().
1–20 rows
Pick a row count and draw the alternating odd/even triangle instantly in the browser.
Complexity
Total prints = rows(rows+1)/2; extra memory stays O(1).
An alternating odd/even number triangle grows each row by one number while switching between odd and even sequences using row parity. With rows = 5, the output is 1, 2 4, 1 3 5, 2 4 6 8, 1 3 5 7 9.
In Java you pick start value k with i % 2, print k in the inner loop, update k += 2, then System.out.println() ends each row.
It combines parity checks with growing row width — a step up from Program 17.
i % 2 picks odd start 1 or even start 2.
Stays odd-only or even-only within each row.
System.out.print(k + " ") in the inner loop; System.out.println() after.
Follow Program 17; continue to Program 19 (fill-with-5 triangle).
In short: for each row i from 1 to rows, set k from i % 2, print k then k += 2 for i numbers, then call System.out.println().
Given a positive integer rows, print an alternating odd/even triangle: odd rows print odd numbers starting at 1, even rows print even numbers starting at 2, each row has i numbers with k += 2.
// rows = 5 (conceptual shape)
// 1
// 2 4
// 1 3 5
// 2 4 6 8
// 1 3 5 7 9 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines to print (typically ≥ 1). |
| Printed output | text | Each row has i spaced numbers — odd or even by row parity. |
for i from 1 to rows:
if i is even: k = 2 else k = 1
for j from 1 to i:
print k + space
k += 2
print newline | Approach | Idea | Best for |
|---|---|---|
| Parity + k += 2 | 1, 2 4, 1 3 5, … | Learning and interviews |
| Ternary start | k = (i % 2 == 0) ? 2 : 1; | Compact user-input version |
| Flip parity | Swap odd/even row assignment | Even rows odd, odd rows even |
| Goal | Pattern |
|---|---|
| Walk each row | for (i = 1; i <= rows; i++) |
| Pick start by parity | if (i % 2 == 0) k = 2; else k = 1; |
| Print and step | System.out.print(k + " "); k += 2; |
| End the row | System.out.println(); |
| Ternary shortcut | int k = (i % 2 == 0) ? 2 : 1; |
| Flip parity rows | int k = (i % 2 == 0) ? 1 : 2; |
Same alternating triangle — different ways to set the row start value k.
parityOdd row → k=1, even row → k=2
sequenceKeeps odd or even within the row
compactOne-line start pick in Example 2
reset kSet k fresh each outer-loop iteration
Reach for this pattern when teaching row parity and the k += 2 sequence inside nested loops.
Natural follow-up after Program 17 — combines parity with growing row width.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare Program 17 (left-shifted odds) and Program 19 (fill-with-5 triangle) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 1 and 20 and draw the alternating odd/even triangle in the browser.
Three complete Java programs — fixed row count, compact ternary user input, and flipped parity variant. Click View Output to reveal sample console results.
Print five rows of the alternating odd/even triangle with i % 2 and k += 2.
rows = 5Hard-coded height — ideal for first demos and screenshots.
public class AlternatingOddEvenTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
int k;
if (i % 2 == 0)
k = 2;
else
k = 1;
for (int j = 1; j <= i; j++) {
System.out.print(k + " ");
k += 2;
}
System.out.println();
}
}
} When i = 1 (odd), k starts at 1 and prints once. When i = 2 (even), k starts at 2 and prints 2 then 4. When i = 3, k runs 1, 3, 5 as 1 3 5, and so on as row width grows. System.out.println() after the inner loop starts the next row.
Read the row count at runtime with Scanner.
Read rows with sc.nextInt(); use a compact ternary for k.
import java.util.Scanner;
public class AlternatingOddEvenInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
int k = (i % 2 == 0) ? 2 : 1;
for (int j = 1; j <= i; j++) {
System.out.print(k + " ");
k += 2;
}
System.out.println();
}
sc.close();
}
} Same nested-loop core as Example 1; only the source of rows changes. The ternary (i % 2 == 0) ? 2 : 1 replaces the if/else block. Non-numeric input leaves rows unset if you ignore Scanner’s return value — always check it in safer labs.
Swap the assignment so even rows print odds and odd rows print evens.
Even rows start at 1 (odds); odd rows start at 2 (evens).
public class AlternatingOddEvenFlipped {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
int k;
if (i % 2 == 0)
k = 1;
else
k = 2;
for (int j = 1; j <= i; j++) {
System.out.print(k + " ");
k += 2;
}
System.out.println();
}
}
} Swap the if/else branches so even rows get k = 1 and odd rows get k = 2. The inner loop and k += 2 logic stay the same — only parity assignment changes.
System.out is built in; use Scanner when reading input. Set rows (fixed or from input).
for (i = 1; i <= rows; i++) makes each row print i numbers.
Set k from i % 2, then System.out.print(k + " ") and k += 2 for i iterations.
System.out.println() ends the row so the next outer iteration starts fresh.
Total prints: rows(rows+1)/2 — O(n²) time, O(1) extra memory.
rows = 4Trace each outer-loop value of i, the starting k, and the numbers printed on each row.
i | Parity | Numbers printed | Row output |
|---|---|---|---|
1 | odd | 1 | 1 |
2 | even | 2, 4 | 2 4 |
3 | odd | 1, 3, 5 | 1 3 5 |
4 | even | 2, 4, 6, 8 | 2 4 6 8 |
Total number prints: 1 + 2 + 3 + 4 = 10 = 4×5/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice System.out.print vs row newline without complex math.
Example: put System.out.println() inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with Scanner return checks and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn i % 2 for row parity first; compare with flipped assignment in Example 3.
Small habits that keep number-pattern code clean.
Use rows (or n) and reset k at the start of each outer-loop iteration.
ScannerCall sc.hasNextInt() so bad input does not leave rows uninitialized.
Only call System.out.println() after the inner loop finishes the row.
Write row i, start k, and each k += 2 step before coding.
Trace rows = 5 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put System.out.println() inside the inner loop.
Mistakes that commonly break alternating odd/even number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use System.out.print(k + " ") for numbers; System.out.println() only after the inner loop.
Reusing k from the previous row mixes odd and even sequences.
→ Set k from i % 2 at the start of each outer-loop iteration.
k++ mixes odd and even numbers within the same row.
→ After each print, update with k += 2 to keep parity.
Omitting System.out.println() glues every number onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave rows uninitialized.
→ Prefer hasNextInt() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n²/2 characters — fine for labs, noisy for huge n.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
k++ mixes odd and even — use k += 2 within each row.
Set k fresh each row from i % 2 — do not carry over from the previous row.
Try these variations to lock in the pattern.
+= 2nrows(rows+1)/2 — O(n²) for n rows.System.out.print(k + " ") stays on the line; System.out.println() advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.Quick Takeaway: outer loop grows row width, set k from i % 2, print k then k += 2, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Flip parity (Example 3) | O(rows²) | O(1) |
The alternating odd/even number triangle is a compact lesson in row parity: i % 2 picks the start value, and k += 2 keeps each row odd-only or even-only. Master the if/else version, then try the ternary and flip-parity variants.
Practice the three examples above, then continue to Program 19 for the fill-with-5 number triangle.
Reset k each row from i % 2 — use k += 2 inside the inner loop and validate rows when reading input.
i % 2 row parity before codingSystem.out.print(k + " ") and reset k each rowrows ≥ 1 for interactive programssc.hasNextInt() before using rowsSystem.out.println() inside the inner digit loopk++ instead of k += 2 within a rowk at the start of each rowrows = 1 edge casePrint the pattern the beginner-friendly way.
i % 2 picks parity
Definition1 for odd rows, 2 for even
CodeStays odd or even
CodeRow i prints i nums
ShapeO(n²) time
AnalysisRow 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.
Move on to the fill-with-5 number triangle in the Java number-pattern series.
12 people found this page helpful