An alternating zigzag number triangle shrinks from rows to 1, printing ascending digits on odd lengths and descending digits on even lengths.
Remember
Rule: outer i = rows..1;
odd i → print 1..i; even i → print i..1
12345
4321
123
21
1 ← rows = 5
Unlike Program 1 (always 1..i) or Program 12 (repeating digits), this pattern flips direction with i % 2 while the row length shrinks.
Approach
How to Solve It
Outer loop from rows down to 1. If i is even, print i..1; otherwise print 1..i. End the line after the inner loop.
Method
Idea
Best for
if / else
Two inner loops chosen by i % 2
Learning, interviews, exams
start / end / step
One inner loop with computed bounds
Compact dry-runs
Pseudocode
Pseudocode
for i from rows down to 1:
if i is even:
for j from i down to 1:
print j
else:
for j from 1 to i:
print j
print newline
Cheat sheet
Goal
Pattern
Outer (shrink)
for (int i = rows; i >= 1; i--)
Odd length
for (int j = 1; j <= i; j++) System.out.print(j);
Even length
for (int j = i; j >= 1; j--) System.out.print(j);
Pick branch
if (i % 2 == 0) { ... } else { ... }
End the row
System.out.println();
One-loop form
Set start, end, step from parity
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit j
System.out.println
Ends the current line
After the inner loop
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the zigzag triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
12345
4321
123
21
1
Trace
Worked Walkthrough
Trace three outer values when rows = 5 — watch parity flip the print direction.
Outer i
Parity
Inner j
Prints
5
odd
1..5
12345
4
even
4..1
4321
3
odd
1..3
123
Row length is always i; only the travel direction of j changes with parity.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and a start/end/step one-loop form. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — outer down from 5, if/else picks ascending or descending.
Java
public class ZigzagNumberTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
if (i % 2 == 0) {
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
} else {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
}
System.out.println();
}
}
}
Output
12345
4321
123
21
1
How It Works
1. Outer shrinks the length.i starts at rows and moves toward 1 — that is how many digits each row gets.
2. Parity picks the direction. Even i prints i..1; odd i prints 1..i.
3. End the row. Call System.out.println() only after the chosen inner loop finishes.
Example 2 — User Input Rows
Read the row count at runtime. Prefer hasNextInt() before nextInt() in real apps.
Java
import java.util.Scanner;
public class ZigzagNumberTriangleInput {
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 = rows; i >= 1; i--) {
if (i % 2 == 0) {
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
} else {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
4321
123
21
1
How It Works
1. Same loop core. Only the source of rows changes from a literal to Scanner.
2. Entering 4 starts even. The first row is length 4, so it opens with 4321.
3. Validate in real apps. Prefer checking hasNextInt() and requiring a positive height (tip below).
Safer input tip
if (!sc.hasNextInt()) {
System.out.println("Enter a positive whole number.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive whole number.");
return;
}
Example 3 — start / end / step
One inner loop — compute direction up front from parity.
Java
public class ZigzagNumberStep {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
int start = (i % 2 == 0) ? i : 1;
int end = (i % 2 == 0) ? 1 : i;
int step = (i % 2 == 0) ? -1 : 1;
for (int j = start; step > 0 ? j <= end : j >= end; j += step) {
System.out.print(j);
}
System.out.println();
}
}
}
Output
12345
4321
123
21
1
How It Works
1. Same zigzag rule. Odd lengths still climb; even lengths still fall.
2. Bounds replace the branch.start, end, and step encode the two if/else loops in one place.
3. Learn the branch first. Prefer Examples 1–2 in class; use this form once the parity rule is clear.
Edge Cases & Pitfalls
Check these before calling the solution done.
Parity
Test i % 2, not the row index from the top
Direction depends on the current length i. When rows is even, the first row is descending.
Swapped branches
Even must print i..1
Swapping the branches flips the zigzag and no longer matches 12345 / 4321.
println inside
Do not put println inside the inner loop
That prints one digit per line and destroys the triangle.
Bad input
Validate with hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require rows >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
if / else (Examples 1–2)
O(n²)
O(1)
start / end / step (Example 3)
O(n²)
O(1)
Digits printed are n + (n − 1) + … + 1 = n(n + 1) / 2 — quadratic in n. Extra memory is only the loop variables.
Remember
Key Takeaways
Rule: outer i = rows..1; odd → 1..i, even → i..1.
Flip:i % 2 chooses direction; length still shrinks every row.
Break the row: call println only after the inner loop finishes.
Complexity:O(n²) time, O(1) extra space.
One line: shrink the row, then let parity decide whether digits climb or fall.
Frequently Asked Questions
An alternating zigzag number triangle: for rows=5 you get 12345, 4321, 123, 21, 1.
That row has length i = 4, which is even. The even branch runs j from i down to 1 and prints 4, 3, 2, 1.
System.out.print(j) stays on the same line. System.out.println() ends the current line. Digits use print; the row break uses println after the inner loop.
Program 12 repeats the row digit (11111, 2222, 333). Program 13 prints sequential digits 1..i or i..1 and flips direction with i % 2.
Program 1 always prints 1..i while shrinking. Program 13 also shrinks, but even lengths reverse to i..1.
Yes. Compute start, end, and step from i % 2, then use one for loop — see Example 3.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
One row prints a single digit 1.
🤔
Did you know?
Odd row length i prints 1..i; even row length prints i..1. The outer loop shrinks from rows to 1, and i % 2 flips direction — still O(n²) total digit prints.