The Inverted Left Triangle (or Inverted Mirrored Right Triangle) is a fantastic exercise for strengthening your grasp on how spaces and characters interact within a grid. By reversing the logic used for a standard Left Triangle, you will learn to manage increasing indentation while decreasing the number of characters printed in each row
What You Will Learn
This tutorial will guide you through creating a C program that prints a right-aligned inverted triangle. You will learn to use a space-printing loop that starts with zero padding and increases with each row, effectively "pushing" the remaining stars to the left of the margin as the triangle narrows
Prerequisites:
Understanding of nested
forloops. Familiarity with the
printffunction and basic loop control.
Final Output:
For a triangle height of 5, the output will look like this:
****
***
**
*
Deconstructing the Pattern: The Logic
1. The Visual Representation
The pattern is composed of two components per row:
Leading Spaces: These increase as the row index increases, shifting the stars to the right
. Stars: These decrease as the row index increases, forming the inverted shape
.
2. Problem Statement
The goal is to print a right-aligned inverted half pyramid. We must calculate the growing number of spaces required for each row and ensure the number of stars printed shrinks proportionally
3. Pattern Analysis & Logic
Outer Loop: Manages the rows, running from i = 1 to n.
Space Loop: To align the stars, we print (i - 1) spaces before the stars in each row
. Star Loop: This loop prints stars, starting from n - i + 1 stars in the first row down to 1 star in the last row
.
The Code Implementation
int main() {
int n = 5; // Height of the triangle
// Outer loop for rows
for (int i = 1; i <= n; i++) {
// Loop to print increasing leading spaces
for (int j = 1; j < i; j++) {
printf(" ");
}
// Loop to print decreasing stars
for (int k = i; k <= n; k++) {
printf("*");
}
// Move to the next line
printf("\n");
}
return 0;
}
Explanation:
Space Loop (
j): The conditionj < icreates the indentation. As row index iincreases, more spaces are printed, pushing the stars to the right. Star Loop (
k): The rangek = itonensures that the number of stars decreases by exactly one with each new row. Newline:
printf("\n")ensures that the program moves to a fresh line after each row is completed.
Sample Output and Analysis
User Input:
The code is set for n = 5.
Program Output:
****
***
**
*
Output Analysis:
In the first row (i=1), the program prints 0 spaces and 5 stars
Common Mistakes and Troubleshooting
Space Logic: A common error is using
j <= iin the space loop, which creates an extra space and shifts the entire triangle out of alignment. Star Loop Range: Ensure your star loop condition is
k <= nto correctly decrement the count as the row index increases.
Complexity Analysis
Time Complexity: O(n^2), given the nested loop structure iterating through the triangular area
. Space Complexity: O(1), as the logic relies on a constant amount of extra memory regardless of input size
.
Conclusion
By balancing the increasing number of spaces with a decreasing number of stars, you have successfully mastered the inverted left triangle. This logic is a critical step toward building more complex, symmetrical patterns like diamonds and hourglass shapes
For all Pattern Programs list click here:
…till the next post, bye-bye & take care
No comments:
Post a Comment