C Number Triangle Pattern (Odd/Even Rows)

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

What Is This Pattern?

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.

How to Solve It

Master row-parity start values first, then optionally flip which rows get odds vs evens.

MethodIdeaBest for
Parity + k += 2Odd rows start at 1; even rows at 2; step by 2Learning, interviews, exams
Flipped paritySwap the starts so even rows print oddsQuick 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

GoalPattern
Grow each rowfor (i = 1; i <= rows; i++)
Pick start kif (i % 2 == 0) k = 2; else k = 1;
Print + stepprintf("%d ", k); k += 2;
End the rowprintf("\n");
Ternary shortcutint k = (i % 2 == 0) ? 2 : 1;

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d ", k)Stays on the same lineEach number k plus a space
printf("\n")Ends the current lineAfter the inner loop

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

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 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 k, and the numbers printed.

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

Total numbers are 1+2+3+4 = 10 — the triangular number n(n+1)/2.

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;
}

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;
}

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;
}

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.

no space

printf("%d", k) without a space

Multi-digit values concatenate (246810). Keep printf("%d ", k) for readable columns.

Time and Space Complexity

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

There are n rows printing 1+2+…+n numbers, so total work is n(n+1)/2.

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.

Next: Fill-with-5 Triangle

Practice a simple filled triangle using the digit 5.

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