Thursday, July 30, 2026

Printing a Inverted Right Triangle | Simple Shape Patterns in C

Building on the foundation of the standard right triangle, the inverted right triangle is a classic exercise that teaches you how to reverse loop boundaries. By shifting from an expanding loop to a shrinking one, you will gain a deeper understanding of how loop conditions dictate the visual output of your C programs.

What You Will Learn

This tutorial will guide you through creating a C program that prints an inverted right triangle of stars. You will learn how to manipulate the inner loop's termination condition to decrease the number of stars printed with each successive row.

Prerequisites:

  • A solid understanding of nested for loops.

  • Familiarity with the printf function.

Final Output:

If we choose a triangle height of 5, the output will appear as follows:

*****
****
***
**
*

Deconstructing the Pattern: The Logic

1. The Visual Representation

In an inverted right triangle, the star count follows a descending sequence:

  • Row 1: 5 stars.

  • Row 2: 4 stars.

  • Row 3: 3 stars.

  • Row 4: 2 stars.

  • Row 5: 1 star.

2. Problem Statement

The objective is to print a pattern of symbols where the number of stars in each row decreases as the row number increases.

3. Pattern Analysis & Logic

  • Identifying the Rows: We use an outer loop to control the vertical height, running from i = 1 to n.

  • Identifying the Columns: We use an inner loop to print stars horizontally. To create the inverted effect, the inner loop runs from j = i to n.

  • Algorithm:

    1. Initialize the size n.

    2. Start an outer loop for the rows (from 1 to n).

    3. Inside, start an inner loop for the columns (from i up to n).

    4. Print the star symbol.

    5. Print a newline character after the inner loop finishes to move to the next row.

The Code Implementation

#include <stdio.h>

int main() {
    int n = 5; // Height of the inverted triangle

    // Outer loop for rows
    for (int i = 1; i <= n; i++) {
        
        // Inner loop prints stars from current row i up to n
        for (int j = i; j <= n; j++) {
            printf("*"); // Print the star symbol
        }
        
        // Move to the next line after each row
        printf("\n"); 
    }

    return 0;
}

Explanation:

  • int n = 5;: Defines the total height of the inverted triangle.

  • for (int i = 1; i <= n; i++): The outer loop tracks the current row index.

  • for (int j = i; j <= n; j++): This logic ensures that as the row index $i$ increases, the number of iterations performed by the inner loop decreases, resulting in fewer stars per row.

  • printf("\n");: Ensures each row of stars begins on a new line.

Sample Output and Analysis

User Input:

  • The code is set for n = 5.

Program Output:

*****
****
***
**
*

Output Analysis: In the first iteration (i=1), the inner loop runs from 1 to 5 (5 stars). In the final iteration (i=5), the inner loop runs from 5 to 5 (1 star), successfully creating the inverted slope.

Complexity Analysis

  • Time Complexity: O(n^2), as the number of operations follows the sum of the first n integers.

  • Space Complexity: O(1), as the logic uses a constant amount of memory.

Conclusion

By reversing the starting point of your inner loop, you have successfully transformed a standard right triangle into an inverted one. This technique is essential for designing more complex patterns, such as diamonds and hourglasses.



For all Pattern Programs list click here

…till the next post, bye-bye & take care


No comments:

Post a Comment