Shape Rule
k / m--
Odd rows print k ascending; even rows print m-- descending.

The alternating triangle prints 1, 3 2, 4 5 6, 10 9 8 7, 11 12 13 14 15 — numbers fill continuously but odd rows ascend and even rows descend. This tutorial covers running counter k, row end m, live preview, worked Java examples, edge cases, and O(n²) complexity.
k / m--
Odd rows print k ascending; even rows print m-- descending.
k marches on
k never resets — it tracks the next number across all rows.
m = k + i - 1
Compute m before each inner loop for even-row descending output.
i % 2
Use i % 2 == 1 to pick ascending vs descending print direction.
3–12 rows
Pick a row count and draw the alternating number triangle instantly in the browser.
Complexity
Total values ≈ n(n+1)/2 — work grows as n².
An alternating ascending/descending number triangle fills numbers continuously from 1, but odd rows print ascending and even rows print descending. Row 2 shows 3 2; row 4 shows 10 9 8 7.
In Java: outer for (i = 1; i <= rows; i++), set m = k + i - 1, inner for (j = 1; j <= i; j++), if odd print k else m--, increment k, then println() after the inner loop.
It is a running-counter exercise that alternates print direction on odd and even rows.
i = 1..rows picks each row number.
k ascending, m descending prints ascending or descending each row.
k tracks the next number; odd rows print k, even rows print from m downward.
Follow Program 50 decreasing-increasing pattern; continue to Program 52 palindrome rows.
In short: outer i=1..rows, m=k+i-1, inner j=1..i, odd print k else m--, k++, then println().
Given rows = 5, print five lines: 1, 3 2, 4 5 6, 10 9 8 7, 11 12 13 14 15.
// rows = 5 (conceptual output)
// 1
// 3 2
// 4 5 6
// 10 9 8 7
// 11 12 13 14 15 | Item | Type | Description |
|---|---|---|
rows | int | How many lines to print (typically ≥ 1). |
i, j | int | Row index i; running counter k and row end m = k + i - 1. |
| Printed output | text | i values on row i — growing triangle shape. |
k = 1
for i from 1 to rows:
m = k + i - 1
for j from 1 to i:
if i odd: print k
else: print m; m = m - 1
k = k + 1
newline | Approach | Idea | Best for |
|---|---|---|
| Running counter k | print(k) on odd rows, print(m--) on even rows | Growing triangle — i values per row |
| Scanner input | sc.nextInt() for rows | User-chosen row count |
| No trailing space | Print space only before 2nd+ values | Cleaner row formatting — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | int rows = 5; |
| Outer loop | for (i = 1; i <= rows; i++) |
| Init counter | int k = 1; |
| Row end | int m = k + i - 1; |
| Inner loop | for (j = 1; j <= i; j++) |
| Odd/even print | if (i % 2 == 1) print(k) else print(m--); then k++ |
| Row break | System.out.println(); after inner loop |
| Program 50 contrast | Decreasing-increasing pattern uses dual loops per row; this pattern uses running counter k with odd/even direction |
How outer row selection, running counter k, row end m, and odd/even direction work together.
for (i = 1; i <= rows; i++)Picks row number i — triangle height.
m = k + i - 1Computed before the inner loop on each row.
if (i % 2 == 1) k
else m--Odd rows ascending from k; even rows descending from m.
trace i=4Dry-run row 4: k=7, m=10 → prints 10 9 8 7.
Reach for this pattern when teaching running counters, odd/even conditions, and alternating row direction.
Classic follow-up after decreasing-increasing patterns like Program 50.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 50 (decreasing-increasing pattern), then continue to Program 52 (palindrome rows).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in running counters, parity checks, and O(n²) thinking.
Choose a row count and draw the alternating number triangle pattern in the browser.
Three complete Java programs — fixed rows = 5, Scanner input, and a no-trailing-space variant. Click View Output to reveal sample console results.
Print five rows with running counter k — odd rows ascending, even rows descending.
rows = 5Hard-coded size — compute m = k + i - 1 and alternate print direction by row parity.
public class AlternatingNumberTriangle {
public static void main(String[] args) {
int rows = 5;
int k = 1;
for (int i = 1; i <= rows; i++) {
int m = k + i - 1;
for (int j = 1; j <= i; j++) {
if (i % 2 == 1) {
System.out.print(k + " ");
} else {
System.out.print(m-- + " ");
}
k++;
}
System.out.println();
}
}
} When i = 4, k = 7, m = 10 — even row prints 10 9 8 7. Row 2 is even — values 2 and 3 print as 3 2.
Read rows with Scanner for flexible output size.
Same k/m counter logic; row count comes from user input.
import java.util.Scanner;
public class AlternatingNumberTriangleInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
int k = 1;
for (int i = 1; i <= rows; i++) {
int m = k + i - 1;
for (int j = 1; j <= i; j++) {
if (i % 2 == 1) System.out.print(k + " ");
else System.out.print(m-- + " ");
k++;
}
System.out.println();
}
sc.close();
}
} Identical counter logic to Example 1; only the row count is dynamic.
Avoid trailing spaces on each row.
Print a space only before the second and later values on each row.
public class AlternatingNumberTriangleNoTrail {
public static void main(String[] args) {
int rows = 5;
int k = 1;
for (int i = 1; i <= rows; i++) {
int m = k + i - 1;
for (int j = 1; j <= i; j++) {
if (j > 1) System.out.print(" ");
if (i % 2 == 1) System.out.print(k);
else System.out.print(m--);
k++;
}
System.out.println();
}
}
} Same counter logic; only the output format avoids trailing spaces.
System.out is built in; use Scanner when reading input. Set rows (e.g. 5).
for (i = 1; i <= rows; i++) — one iteration per output line.
Set m = k + i - 1. If i is odd, print k; if even, print m--. Increment k each inner step.
System.out.println(); after the inner loop moves to the next row.
Total prints ≈ n(n+1)/2 — O(n²) time, O(1) extra memory.
i = 4Trace row 4 with rows = 5 to see how k, m, and even-row descending output build 10 9 8 7.
| Step | k | m | Printed |
|---|---|---|---|
| Before row 4 | 7 | — | — |
| Compute m | 7 | 10 | — |
| j=1 (even row) | 8 | 9 | 10 |
| j=2 | 9 | 8 | 9 |
| j=3 | 10 | 7 | 8 |
| j=4 | 11 | 6 | 7 |
Row output: 10 9 8 7. Then println() moves to row 5 with k = 11.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use Scanner for dynamic row count — see Example 2.
Each row alternates print direction while k marches forward continuously.
Example: compare row 4 (10 9 8 7) — even row prints from m downward.
Practice println vs print for multi-line vs single-line output.
Example: use print(k + " ") in both loops for spaced output — Example 3.
Print a space only before the second and later values on each row.
Example: print rows=5 without trailing spaces and compare formatting.
Growing inner bounds plus direction flip makes O(n²) concrete for beginners.
Example: count values for rows=5 → 1+2+3+4+5 = 15 printed numbers.
Pair the pattern with Scanner and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Java courses.
Wrong odd/even direction shows immediately — even rows should print descending from m.
Only loops and console output — no arrays or math libraries.
Change rows, use Scanner, or remove trailing spaces with conditional print.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the running counter k first; then try Scanner input and the spaced-digit variant in Example 3.
Small habits that keep number-pattern code clean.
Use rows for height and i/j for row/column indices.
ScannerAvoid crashes when the user types letters instead of a number.
Compute m, run inner loop with direction check, increment k, then println().
Use print(k + " ") or print(m-- + " ") inside the inner loop; one println() per row after it finishes.
Trace rows = 5, i = 4 on paper — expect 10 9 8 7.
Pro Tip: if even rows look ascending, check whether you forgot m = k + i - 1 or the odd/even condition.
Mistakes that commonly break alternating ascending/descending number triangles.
Each value lands on its own line — you get a column, not a triangle row.
→ Use print inside the inner loop; println() only after it finishes.
Without computing m before the inner loop, even rows cannot print descending correctly.
→ Always set m = k + i - 1 before the inner loop on every row.
Omitting println() after the inner loop glues all rows onto one line.
→ Always call System.out.println() after the inner loop completes.
Resetting k to 1 each row breaks the continuous number sequence.
→ Let k continue across rows; only compute fresh m each outer iteration.
Letters or empty input throw InputMismatchException.
→ Use sc.hasNextInt() before sc.nextInt().
Using literal 10 in loop bounds instead of variable rows breaks dynamic input.
→ Use one rows variable for the outer loop bound.
Check these inputs before calling the solution done.
Output is one line: 1.
Loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Large row counts produce many values — fine for labs; use smaller n for quick demos.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
rows = 3, 6, or 83 2 to 2 3for (i = rows; i >= 1; i--)n(n+1)/2 (e.g. 15 values for rows=5).print stays on the line; println advances — mix them carefully.rows > 0 for interactive programs; rows = 1 prints one value.print(k); even rows use print(m--) — always increment k each inner step.Quick Takeaway: outer i=1..rows, m=k+i-1, inner j=1..i, odd print k else m--, k++, then println().
| Program | Time | Extra space |
|---|---|---|
| Fixed rows = 5 (Example 1) | O(n²) | O(1) |
| Scanner input (Example 2) | O(n²) | O(1) |
| No trailing space (Example 3) | O(n²) | O(1) |
The alternating triangle combines a running counter with odd/even row direction — a natural step after decreasing-increasing patterns. Master the fixed-rows version first, then try Scanner input and the no-trailing-space variant in Example 3.
Practice the three examples above, then continue to Program 52 for the increasing-decreasing palindrome pattern (1, 232, 34543…).
Keep println() after the inner loop — one row break per outer iteration.
i, running counter k, row end m = k + i - 1, and odd/even direction before codingprint(k + " ") or print(m-- + " "), then println() after inner looprows ≥ 1 for interactive programsScanner return value before using rowsk each row (breaks continuous sequence)m = k + i - 1 before the inner looprowsrows = 1 edge casePrint the pattern the beginner-friendly way.
Each row prints i values
i = 1..rows
CodeOdd: print k; even: print m--
Logicn(n+1)/2 prints
O(n²)
AnalysisNumbers fill continuously from 1, but each row alternates print direction — odd rows ascending (4 5 6), even rows descending (10 9 8 7). Compute row end with m = k + i - 1.
Move on to the increasing-decreasing palindrome pattern in the Java number-pattern series.
12 people found this page helpful