After mastering the solid full pyramid, we can introduce a new level of complexity: the hollow pattern. Printing a hollow alphabet triangle (half pyramid) is an excellent exercise in using if-else conditions within your loops to control precisely when a character is printed and when a space is left empty
What You Will Learn
This tutorial will guide you through creating an alphabet triangle that is hollow, meaning only the outer edges are filled with letters, while the interior remains empty. You will learn to use logical conditions to determine whether to print a character or a space
Prerequisites:
Strong understanding of nested
forloops. Familiarity with conditional (
if-else) statements.
Final Output:
For a 5-row triangle, the output will look like this:
A
AB
A C
A D
ABCDE
Deconstructing the Pattern: The Logic
1. The Visual Representation
To create the hollow effect, we identify the specific positions where we must print a letter:
First Column: Always print 'A' (index 0)
. Last Column: Print the character corresponding to the current row index
. Bottom Row: Print the full sequence 'A' through the end character
. Interior: Print spaces to create the "hollow" look
.
2. Problem Statement
The goal is to print a half pyramid where the internal characters are replaced by empty spaces, except for the boundary edges and the base row
3. Pattern Analysis & Logic
Outer Loop: Manages the rows, running from $i = 0$ to $n-1$
. Inner Loop: Runs from $j = 0$ to $i$
. Printing Condition: Print the character
'A' + jif:It is the first column (
j == 0). It is the last column of the row (
j == i). It is the last row of the triangle (
i == n - 1). Otherwise, print a space
.
The Code Implementation
#include <stdio.h>
int main() {
int n = 5; // Total number of rows
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
// Check boundary conditions for the hollow effect
if (j == 0 || j == i || i == n - 1) {
printf("%c", 'A' + j);
} else {
printf(" "); // Print space for the interior
}
}
printf("\n"); // Move to the next line
}
return 0;
}
Explanation:
ifCondition: The conditionj == 0 || j == i || i == n - 1ensures that characters are only printed on the left edge, the right slope, and the very bottom line. elseStatement: This handles the interior of the pyramid, printing a space to keep the structure hollow.
Sample Output and Analysis
User Input:
The code is configured for
n = 5.
Program Output:
AB
A C
A D
ABCDE
Output Analysis:
The boundary logic keeps the edges intact. For instance, in row 3 (i=3), the loop prints 'A' at j=0, a space at j=1 and j=2, and finally 'D' at j=3
Common Mistakes and Troubleshooting
Logical OR (
||): A common error is using&&(AND) instead of||(OR) in theifstatement, which will cause the program to print nothing, as all conditions cannot be true at once. Space Printing: Ensure you use
printf(" ");with a space character; omitting the space will cause the triangle to collapse.
Complexity Analysis
Time Complexity: O(n^2), as the nested structure iterates through the triangular grid
. Space Complexity: O(1), as the memory usage remains constant
.
Conclusion
By adding conditional logic to your inner loop, you have moved from solid shapes to sophisticated hollow patterns. This technique is essential for building complex, visually interesting designs in C
For all Pattern Programs list click here:
…till the next post, bye-bye & take care