Combining character arithmetic with conditional checks inside nested loops provides a dynamic way to generate formatted text structures
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 rowsA, E, I, O, U)U, it wraps around to start again at A
Pattern Analysis & Logic
Rows (Outer Loop):
iruns from1ton. Columns (Inner Loop):
jruns from1toi, dictating the total characters printed in each row. Conditional Character Filtering: We evaluate standard uppercase characters (
A–Z) 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
Prompt the user to enter the number of rows
n. Initialize a tracking character variable
ch = 'A'. Execute an outer loop with control variable
irunning from1ton. Execute an inner loop with control variable
jrunning from1toi. Inside the inner loop, use a
whileloop to find the next vowel:Test if
chis a vowel. If it is not a vowel, increment
ch(wrapping to'A'if it exceeds'Z') and re-check.
Print the valid vowel followed by a space, increment
ch(handling wrap-around), and complete the inner iteration. Print a newline character (
\n) after completing each row.
Code Implementation
#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 returningtrueifcmatches 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
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 % 2to 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'whenchexceeds'Z'causes non-alphabetic ASCII characters (like[,\,]) to appear in your pattern. Missing Counter Increment: Failing to increment
chafter 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