Alternating Odd & Even Number Pattern in C

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
If-Else + Nested Loops

What You’ll Learn

How to print an alternating odd and even number triangle in C. Odd rows print odd numbers (1 3 5 ...) and even rows print even numbers (2 4 6 ...).

This is a nice pattern to practice nested loops plus a tiny rule using i % 2 to decide whether each row starts at 1 or 2.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9
1

Complete C Program

We pick the row start (k) based on whether the row number is odd or even, then add 2 each time to keep the parity.

c
#include <stdio.h>

int main() {
    int rows = 5;
    int i, j, k;

    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

Pick how many rows to print

int rows = 5; controls the triangle height.

Setup
2

Outer loop controls rows

for (i = 1; i <= rows; ++i) prints one line per iteration.

Row control
3

Choose odd or even start

If the row is even, start at 2; otherwise start at 1: k = (i % 2 == 0) ? 2 : 1;

Parity rule
4

Inner loop prints i numbers

for (j = 1; j <= i; ++j) prints k, then does k += 2 to stay odd/even.

Number printing
=

Alternating odd/even triangle

Total prints across all rows is 1+2+…+n = n(n+1)/2, so the time complexity is O(n²) for n rows.

2

Variation — User Input Version

Make the row count dynamic by reading it from the user using scanf():

c
#include <stdio.h>

int main() {
    int rows;
    int i, j, k;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

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

💡 Tips for Enhancement

Try These

  • Accept row count from the user with scanf()
  • Reverse the rule to start odd rows with even numbers
  • Print without trailing spaces by handling the last column separately
  • Right-align the triangle by printing leading spaces
  • Swap numbers with characters to create alternating alphabet patterns

Avoid

  • Forgetting the newline after each row
  • Mixing up row parity (odd/even) logic
  • Using k++ (it breaks the odd/even sequence)
  • Allowing negative row counts without validation
  • Hard-coding row count when a user-input version is desired

Key Takeaways

1

Odd rows start with 1 and print odd numbers: 1, 3, 5…

2

Even rows start with 2 and print even numbers: 2, 4, 6…

3

k += 2 preserves parity across the row.

4

This pattern mixes if-else with nested loops — a common interview combo.

❓ Frequently Asked Questions

Because the start value changes by row: odd rows start at 1 and even rows start at 2, so the rows alternate between odd and even sequences.
Adding 2 keeps the row values all odd or all even. Starting from 1 gives 1, 3, 5… and starting from 2 gives 2, 4, 6…
Yes. Swap the starting value assignment: use k = 1 for even rows and k = 2 for odd rows.
O(n²) for n rows, because you print 1+2+…+n numbers in total.

Explore More C Number Patterns!

Keep practicing nested loops with patterns that add alignment, parity, and spacing rules.

All Number Patterns →
Did you know?

You can generalize this idea to many patterns: pick a row rule (odd/even, prime/composite, increasing/decreasing) and keep the inner loop responsible only for printing the right count for that row.

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