Printing a mirrored triangle is the perfect way to test your understanding of space management in C. While standard triangles start at the left margin, a mirrored triangle requires us to "push" the characters to the right by printing leading spaces first. This is a foundational exercise for mastering complex alignment in pattern programming
What You Will Learn
This tutorial will guide you through creating a C program that prints a right-aligned alphabet triangle. You will learn how to use a dedicated inner loop to print the necessary padding spaces before your character sequence
Prerequisites:
Solid understanding of nested
forloops. Basic knowledge of printing characters using
'A' + j.
Final Output:
For a 5-row triangle, the output will look like this:
A
AB
ABC
ABCD
ABCDE
Deconstructing the Pattern: The Logic
1. The Visual Representation
To create a mirror effect, each row is composed of two distinct sections:
Leading Spaces: These decrease as the row number increases
. Characters: These increase as the row number increases
.
2. Problem Statement
The goal is to print a right-aligned half pyramid. We must calculate the number of spaces required for each row and then print the corresponding sequence of characters
3. Pattern Analysis & Logic
Outer Loop: Manages the rows, running from $i = 0$ to $n-1$
. Space Loop: To align the characters, we print $(n - i - 1)$ spaces before the characters in each row
. Character Loop: This loop prints the characters from 'A' up to the current row index
.
The Code Implementation
int main() {
int n = 5; // Total number of rows
for (int i = 0; i < n; i++) {
// Loop to print leading spaces
for (int k = 0; k < n - i - 1; k++) {
printf(" ");
}
// Loop to print characters
for (int j = 0; j <= i; j++) {
printf("%c", 'A' + j);
}
// Move to the next line
printf("\n");
}
return 0;
}
Explanation:
Space Loop (
k): The formulan - i - 1ensures that as the row index $i$ increases, the number of spaces printed decreases, creating the "mirror" slope. Character Loop (
j): This prints the alphabet sequence. Because the space loop runs first, the characters appear right-aligned. Newline:
printf("\n")is placed outside both inner loops to ensure each row starts on a fresh line.
Sample Output and Analysis
User Input:
The code is set for
n = 5.
Program Output:
AB
ABC
ABCD
ABCDE
Output Analysis:
In the first row (i=0), 4 spaces are printed followed by 'A'. In the final row (i=4), 0 spaces are printed followed by 'ABCDE'. This combination effectively pushes the triangle to the right
Common Mistakes and Troubleshooting
Space Calculation: A common error is using
n - iinstead ofn - i - 1for the space loop, which shifts the entire triangle one position to the right. Print Specifier: Ensure you use
printf(" ")with a space character inside the quotes for the padding loop.
Complexity Analysis
Time Complexity: O(n^2), as the nested loops iterate proportional to the square of the input size
. Space Complexity: O(1), as the logic uses a constant amount of extra space
.
Conclusion
By adding a space-handling loop before your character printing logic, you have successfully mirrored your pyramid. This is a vital technique for creating more sophisticated shapes like full pyramids and diamonds
For all Pattern Programs list click here:
…till the next post, bye-bye & take care
No comments:
Post a Comment