Tuesday, September 8, 2026

Printing Alternating Even/Odd Number Triangle In C | Conditional Patterns

Integrating conditional statements into dynamic loop structures allows you to manipulate matrix-like numerical outputs with precision. Today's entry in our Pattern Programming in C series covers the Alternating Even/Odd Number Triangle. This tutorial demonstrates how to use parity logic to dynamically alter printed sequences row by row.

Pattern Title: Alternating Even/Odd Number Triangle Pattern in C

Purpose: Deepen your understanding of nested loops, modulus operations, and row-level parity switching.

Prerequisites: Familiarity with nested for loops, printf, scanf, and basic if-else control flow.

Final Output

For an input of n = 5 rows, the program generates:

2
1 3
2 4 6
1 3 5 7
2 4 6 8 10

Deconstructing the Pattern: The Logic

Problem Statement

Write a C program that accepts an integer n for the total number of rows. The program must generate a triangle where even rows (1st, 3rd, 5th, etc.) print consecutive even numbers, and odd-indexed logic (or alternating rows) displays consecutive odd numbers starting from 1.

Pattern Analysis & Dynamic Logic

  • Row Loop (Outer Loop): i runs from 1 to n.

  • Column Loop (Inner Loop): j runs from 1 to i, dictating the total numbers printed per row.

  • Conditional Parity Check:

    • If the row index i is odd, print the jth even number: 2 \times j.

    • If the row index i is even, print the jth odd number: (2 \times j) - 1.

Step-by-Step Algorithm

  1. Read the positive integer n from the user.

  2. Iterate i from 1 up to n for row placement.

  3. For each row i, iterate j from 1 to i.

  4. Evaluate i % 2:

    • If non-zero (odd row), evaluate value as 2 * j.

    • If zero (even row), evaluate value as (2 * j) - 1.

  5. Print the calculated number followed by a space.

  6. Print a newline (\n) at the end of every row iteration.

Code Implementation

#include <stdio.h>

int main() {
    int n;

    // Prompt user for input
    printf("Enter the number of rows: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Please enter a valid positive integer.\n");
        return 1;
    }

    // Outer loop for rows
    for (int i = 1; i <= n; i++) {
        // Inner loop for columns in row 'i'
        for (int j = 1; j <= i; j++) {
            // Check row index parity
            if (i % 2 != 0) {
                // Odd rows print even numbers
                printf("%d ", 2 * j);
            } else {
                // Even rows print odd numbers
                printf("%d ", (2 * j) - 1);
            }
        }
        // Move to the next line after completing the row
        printf("\n");
    }

    return 0;
}

Line-by-Line Code Breakdown

  • if (scanf("%d", &n) != 1 || n <= 0): Performs strict input validation to guard against non-numeric or non-positive input values.

  • for (int i = 1; i <= n; i++): Dictates row progression from top to bottom.

  • if (i % 2 != 0): Determines whether the current row index is odd or even.

  • printf("%d ", 2 * j);: Computes standard even numbers sequentially per column.

  • printf("%d ", (2 * j) - 1);: Computes standard odd numbers sequentially per column.

  • printf("\n");: Terminates the current printed line to build the triangular shape.

Compiling & Execution

Sample Output

Enter the number of rows: 4
2 
1 3 
2 4 6 
1 3 5 7 

Variations & Enhancements

  • Column-Based Alternating: Replace row checks (i % 2) with column checks (j % 2) to alternate numbers across columns within the exact same row.

  • Inverted Alternating Triangle: Reverse the outer loop (for (int i = n; i >= 1; i--)) to print the largest row at the top.

  • Continuous Global Counter: Instead of resetting calculations per row using j, maintain global even and odd counters across the entire execution.

Common Mistakes & Troubleshooting

  • Confusing Row (i) vs. Column (j) Parity: Checking j % 2 instead of i % 2 changes the output from alternating rows to alternating columns.

  • Incorrect Odd Formula: Using 2 * j + 1 skips 1 and starts odd rows at 3. Ensure you use (2 * j) - 1 when j starts at 1.

  • Missing Line Break: Forgetting printf("\n"); renders all numbers into a single continuous stream.

Complexity Analysis

  • Time Complexity: {O}(n^2) — requires n(n+1)/2 total operations across outer and inner loops.

  • Space Complexity: {O}(1) — executes in constant space using basic scalar variables.

Mastering parity-based conditional checks inside nested structures gives you complete control over complex matrix layouts.

Try rewriting this program so odd rows print odd numbers and even rows print even numbers! Share your code modifications in the comments below.



For all Pattern Programs list click here

…till the next post, bye-bye & take care 

No comments:

Post a Comment