Combining character manipulation with row-level conditional checks allows you to build dynamic, formatted text arrangements in C
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 rowsA, B, C...) and even-numbered rows display lowercase letters (a, b, c...)
Pattern Analysis & Dynamic Logic
Rows (Outer Loop): Control variable
iruns from1ton. Columns (Inner Loop): Control variable
jruns from1toi, controlling how many characters appear in the current row. Character Tracking: Maintain a continuous offset or global sequence tracker (
letter_indexstarting at0for'A'/'a').Conditional Parity Check:
If the row index
iis odd (i % 2 != 0), output the character in uppercase:'A' + letter_index. If the row index
iis 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
Prompt the user to enter the number of rows
n. Initialize an integer tracking variable
letterIndex = 0. Execute an outer loop
ifrom1ton. Execute an inner loop
jfrom1toi. 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).
Print the computed character followed by a space and increment
letterIndex. Output a newline character (
\n) after each row completes.
Code Implementation
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
A
b c
D E F
g h i j
Variations & Enhancements
Column-Based Alternating Case: Evaluate
j % 2instead ofi % 2to 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 % 2instead ofi % 2toggles 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