Saturday, September 12, 2026

Printing Alternating Alphabet Patterns in C | Conditional Patterns

Combining character manipulation with row-level conditional checks allows you to build dynamic, formatted text arrangements in C. Continuing our Pattern Programming in C series, today’s article focuses on the Alternating Alphabet Triangle. This tutorial demonstrates how to use nested loops and parity logic to switch character cases or sequence types across alternating rows.

Pattern Title: Alternating Case Alphabet Triangle Pattern in C

Purpose: Strengthen your understanding of nested loops, ASCII character arithmetic, and row-index parity logic.

Prerequisites: Knowledge of basic for loops, printf, scanf, standard character types (char), and if-else control flow.

Final Output

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

b c 
D E F 
g h i j 
K L M N O 

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 right-angled triangle filled sequentially with alphabetic letters, where odd-numbered rows display uppercase letters (A, B, C...) and even-numbered rows display lowercase letters (a, b, c...).

Pattern Analysis & Dynamic Logic

  • Rows (Outer Loop): Control variable i runs from 1 to n.

  • Columns (Inner Loop): Control variable j runs from 1 to i, controlling how many characters appear in the current row.

  • Character Tracking: Maintain a continuous offset or global sequence tracker (letter_index starting at 0 for 'A'/'a').

  • Conditional Parity Check:

    • If the row index i is odd (i % 2 != 0), output the character in uppercase: 'A' + letter_index.

    • If the row index i is even (i % 2 == 0), output the character in lowercase: 'a' + letter_index.

  • Character Reset: Wrap the alphabet back to the start (index % 26) to avoid non-alphabetic ASCII symbols when iterating past 26 characters.

Step-by-Step Algorithm

  1. Prompt the user to enter the number of rows n.

  2. Initialize an integer tracking variable letterIndex = 0.

  3. Execute an outer loop i from 1 to n.

  4. Execute an inner loop j from 1 to i.

  5. Evaluate i % 2:

    • If i % 2 != 0 (odd row), calculate character as 'A' + (letterIndex % 26).

    • If i % 2 == 0 (even row), calculate character as 'a' + (letterIndex % 26).

  6. Print the computed character followed by a space and increment letterIndex.

  7. Output a newline character (\n) after each row completes.

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

    int letterIndex = 0;

    // 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 parity for alternating case
            if (i % 2 != 0) {
                // Odd rows: Uppercase
                printf("%c ", 'A' + (letterIndex % 26));
            } else {
                // Even rows: Lowercase
                printf("%c ", 'a' + (letterIndex % 26));
            }
            letterIndex++;
        }
        // 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): Ensures clean input validation by checking for non-numeric or non-positive integer values.

  • int letterIndex = 0;: Serves as a continuous offset tracker across all rows.

  • if (i % 2 != 0): Evaluates whether the current row index is odd or even to select character casing.

  • 'A' + (letterIndex % 26): Uses standard ASCII arithmetic to derive the correct uppercase character while wrapping around at 26 letters.

  • 'a' + (letterIndex % 26): Derives the matching lowercase character for even rows.

  • printf("\n");: Terminates the current row to maintain proper triangle geometry.

Compiling & Execution

Sample Run

Enter the number of rows: 4
A 
b c 
D E F 
g h i j 

Variations & Enhancements

  • Column-Based Alternating Case: Evaluate j % 2 instead of i % 2 to alternate uppercase and lowercase characters across columns within the exact same row.

  • Alternating Alphabet Directions: Reverse letter placement on even rows to print forward on odd rows (A, B, C) and backward on even rows (f, e, d).

  • Inverted Triangle: Reverse the outer loop (for (int i = n; i >= 1; i--)) to output a top-heavy triangular pattern.

Common Mistakes & Troubleshooting

  • ASCII Overflow: Forgetting modulo arithmetic (% 26) will cause printing to extend into non-alphabetic ASCII characters (such as [, \, ]) when total letters exceed 26.

  • Confusing Row vs. Column Parity: Checking j % 2 instead of i % 2 toggles casing per character rather than per row.

  • Missing Line Break: Leaving out printf("\n"); outputs the entire character series as a single line.

Complexity Analysis

  • Time Complexity: {O}(n^2) — requires n(n+1)/2 total iterations across nested loops.

  • Space Complexity: {O}(1) — operates strictly using constant memory space.

Combining character arithmetic with conditional parity checks provides flexible control over ASCII output layouts.

Try modifying the program to print alternating alphabet rows forward and backward! Post your implementation in the comments below.



For all Pattern Programs list click here

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