Java Number Triangle Pattern (Odd/Even Rows)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An alternating odd/even number triangle grows by one number each row, but switches between odd-only and even-only sequences based on the row’s parity.

Remember
Rule: odd rows start at 1; even rows start at 2;
      then k += 2 for the rest of the row

1
2 4
1 3 5
2 4 6 8
1 3 5 7 9     ← 5 rows

Row width still grows like a classic triangle (1, 2, 3, … numbers per row). The twist is the start value from i % 2 and the step of +2 that keeps parity fixed on each line.

How to Solve It

Pick the start with row parity, then print a growing odd or even stream with k += 2.

MethodIdeaBest for
if / else startOdd → k = 1; even → k = 2Learning, interviews, exams
Ternary startk = (i % 2 == 0) ? 2 : 1Shorter demos once parity clicks

Pseudocode

Pseudocode
for i from 1 to rows:
    if i is even:
        k = 2
    else:
        k = 1
    for j from 1 to i:
        print k
        k = k + 2
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (int i = 1; i <= rows; i++)
Pick startint k = (i % 2 == 0) ? 2 : 1;
Grow widthfor (int j = 1; j <= i; j++)
Print + stepSystem.out.print(k + " "); k += 2;
End the rowSystem.out.println();
Flip parityEven → k = 1; odd → k = 2

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach number (and its trailing space)
System.out.printlnEnds the current lineAfter the inner loop

Print numbers without a newline, then end the row once.

Live Preview

Change the height and the odd/even triangle updates instantly — including the triangular number total.

Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 numbers
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9

Worked Walkthrough — rows = 4

Trace each row’s parity, start value, and printed numbers.

iParityStart kPrinted row
1odd11
2even22 4
3odd11 3 5
4even22 4 6 8

Total numbers: 1 + 2 + 3 + 4 = 10 = 4×5/2. That triangular sum is why time is O(n²).

Java Programs

Three complete programs: fixed 5 rows, Scanner input with a ternary start, and flipped parity. Use View Output to reveal sample results.

Example 1 — Fixed 5 Rows

Hard-coded height — if/else picks the start; the inner loop prints and steps by 2.

Java
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();
        }
    }
}

How It Works

1. Outer loop grows width. Row i prints exactly i numbers.

2. Parity picks the start. Even i → k = 2; odd i → k = 1.

3. Inner loop prints and steps. Print k, then k += 2 so the row stays odd-only or even-only.

When i = 3, k runs 1, 3, 5 → 1 3 5.

Example 2 — Row Count Input

Read the height at runtime. Prefer hasNextInt() before nextInt() in real apps.

Java
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();
    }
}

How It Works

1. Prompt and read. Ask for a row count, then store it with sc.nextInt().

2. Same core, shorter start. The ternary (i % 2 == 0) ? 2 : 1 replaces the if/else block.

3. Safer input tip. Prefer:

Safer input
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 — Flipped Row Parity

Even rows print odds; odd rows print evens — only the start assignment changes.

Java
public class AlternatingOddEvenFlipped {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            int k = (i % 2 == 0) ? 1 : 2;

            for (int j = 1; j <= i; j++) {
                System.out.print(k + " ");
                k += 2;
            }
            System.out.println();
        }
    }
}

How It Works

1. Swap the start map. Even i → k = 1; odd i → k = 2.

2. Same print + step. Inner loop and k += 2 are unchanged.

3. Same widths. Only which sequence lands on odd vs even rows flips.

Edge Cases & Pitfalls

Check these before calling the solution done.

k += 1

Mixed parity

If you use k++ instead of k += 2, rows mix odds and evens. Keep the step at 2.

Wrong start

All odds or all evens

Forgetting the parity check and always starting at 1 (or 2) loses the alternating pattern.

println inside

Column of numbers

If println is inside the inner loop, each number lands on its own line. Use print for numbers; println only after the inner loop.

rows = 1

Single 1

Output is just 1 — a good sanity check for the odd-start path.

rows ≤ 0

Empty output

The outer loop never runs. Validate and require rows ≥ 1.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive whole number.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)

Total numbers printed = 1 + 2 + … + n = n(n + 1)/2 — still quadratic in n.

Key Takeaways

  • Rule: odd rows start at 1; even rows start at 2; then step by 2.
  • Width grows: row i prints exactly i numbers.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row, pick start from parity, print i numbers stepping by 2, then println().

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.
System.out.print(k + " ") stays on the same line with a trailing space. System.out.println() ends the current line. Numbers use print; the row break uses println 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. Swap the condition so even rows start at 1 and odd rows start at 2 — Example 3 on this page shows that flip.
O(n²) for n rows. Total numbers printed equal 1+2+…+n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt(), require n ≥ 1, and reject non-numeric input — see Example 2 notes.

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.

Next: Fill with 5 Triangle

Move on to the fill-with-5 number triangle in the Java number-pattern series.

Program 19 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful