Friday, September 11, 2026

Printing Conditional Alphabet Patterns (e.g., Vowels/Consonants) in C | Conditional Patterns

Combining character arithmetic with conditional checks inside nested loops provides a dynamic way to generate formatted text structures. Today's article in our Pattern Programming in C series covers Conditional Alphabet Patterns. This tutorial demonstrates how to apply logic to filter characters—specifically isolating vowels—to build a customized triangular pattern.

Pattern Title: Vowels-Only Triangle Pattern in C

Purpose: Enhance your nested loop logic, character handling, and conditional reasoning by generating alphabet sequences filtered by specific properties.

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

Final Output

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

E I 
O U A 
E I O U 

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 print a right-angled triangle where each position is filled sequentially by uppercase English vowels (A, E, I, O, U). When the sequence reaches U, it wraps around to start again at A.

Pattern Analysis & Logic

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

  • Columns (Inner Loop): j runs from 1 to i, dictating the total characters printed in each row.

  • Conditional Character Filtering: We evaluate standard uppercase characters (AZ) sequentially. A character is printed only if it passes a vowel check (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U').

  • Character Reset: After finding and printing a vowel, we advance to the next character in the alphabet, wrapping back to 'A' after 'Z'.

Step-by-Step Algorithm

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

  2. Initialize a tracking character variable ch = 'A'.

  3. Execute an outer loop with control variable i running from 1 to n.

  4. Execute an inner loop with control variable j running from 1 to i.

  5. Inside the inner loop, use a while loop to find the next vowel:

    • Test if ch is a vowel.

    • If it is not a vowel, increment ch (wrapping to 'A' if it exceeds 'Z') and re-check.

  6. Print the valid vowel followed by a space, increment ch (handling wrap-around), and complete the inner iteration.

  7. Print a newline character (\n) after completing each row.

Code Implementation

#include <stdio.h>
#include <stdbool.h>

// Helper function to check if a character is an uppercase vowel
bool isVowel(char c) {
    return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}

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

    char ch = 'A';

    // 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++) {
            // Advance until a vowel is found
            while (!isVowel(ch)) {
                ch++;
                if (ch > 'Z') {
                    ch = 'A';
                }
            }
            
            // Print the vowel
            printf("%c ", ch);

            // Move to next character for future iterations
            ch++;
            if (ch > 'Z') {
                ch = 'A';
            }
        }
        // Move to the next line after completing the row
        printf("\n");
    }

    return 0;
}

Line-by-Line Code Breakdown

  • bool isVowel(char c): A helper function returning true if c matches any uppercase vowel.

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

  • while (!isVowel(ch)): Skips non-vowel characters until the next valid vowel is reached.

  • if (ch > 'Z') { ch = 'A'; }: Ensures the character pointer safely wraps back to the beginning of the alphabet without outputting non-alphabetic ASCII symbols.

  • printf("%c ", ch);: Outputs the target character followed by spacing for alignment.

  • printf("\n");: Moves execution to the next line after each row finishes.

Compiling & Execution

Sample Run

Enter the number of rows: 5
A 
E I 
O U A 
E I O U 
A E I O U 

Variations & Enhancements

  • Consonants-Only Triangle: Invert the conditional check (!isVowel(ch)) to generate patterns composed strictly of consonant letters.

  • Alternating Vowel/Consonant Rows: Toggle the condition on alternating rows using i % 2 to print vowels on odd rows and consonants on even rows.

  • Lowercase Conversion: Adapt the character check and range ('a' to 'z') to render the pattern in lowercase.

Common Mistakes & Troubleshooting

  • ASCII Overflow: Forgetting to wrap back to 'A' when ch exceeds 'Z' causes non-alphabetic ASCII characters (like [, \, ]) to appear in your pattern.

  • Missing Counter Increment: Failing to increment ch after printing causes the loop to get stuck on the same character infinitely.

  • Missing Line Break: Forgetting printf("\n"); outputs all characters onto a single continuous line.

Complexity Analysis

  • Time Complexity: {O}(n^2) — requires n(n+1)/2 total character output operations, with minor constant iterations for character filtering.

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

Mastering character arithmetic alongside conditional filtering opens up powerful possibilities for string and text-based pattern generation.

Try altering the condition to build a Consonants-Only Pattern! Share your code implementation in the comments below.



For all Pattern Programs list click here

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

No comments:

Post a Comment