Print an Inverted Right-Angled Triangle Star Pattern in C

To print an inverted right-angled triangle star pattern in C, we use nested loops. The outer loop controls the number of rows, and the inner loop prints stars (*) based on the current row index. The number of stars decreases as we move to the next row, forming an inverted triangle.


Examples to Print an Inverted Right-Angled Triangle Star Pattern

1. Basic Inverted Right-Angled Triangle Pattern

In this example, we will print a simple inverted right-angled triangle where the number of stars in each row decreases from n to 1.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n = 5; // Number of rows

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer variable n and assign it the value 5 (number of rows).
  2. The outer loop (for (int i = n; i >= 1; i--)) runs from n to 1, controlling the rows.
  3. The inner loop (for (int j = 1; j <= i; j++)) prints i stars in each row.
  4. printf("* ") prints a star with space.
  5. printf("\n") moves to the next line after printing stars.

Output:

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

2. Inverted Right-Angled Triangle with User Input

In this example, we allow the user to input the number of rows, making the pattern dynamic.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n;
    
    printf("Enter the number of rows: ");
    scanf("%d", &n);

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer variable n to store user input.
  2. printf("Enter the number of rows: ") prompts the user for input.
  3. scanf("%d", &n) reads the user input.
  4. The outer loop (for (int i = n; i >= 1; i--)) controls the number of rows.
  5. The inner loop (for (int j = 1; j <= i; j++)) prints stars in decreasing order.
  6. printf("\n") ensures the next row starts on a new line.

Output:

When the program is run, prompt appears to enter the number of rows. Let us say that we enter 4.

Enter the number of rows: 4
* * * * 
* * * 
* * 
*

3. Right-Aligned Inverted Right-Angled Triangle Pattern

In this example, we will print an inverted right-angled triangle where the stars are right-aligned. This is achieved by printing leading spaces before the stars in each row.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n = 5; // Number of rows

    for (int i = n; i >= 1; i--) {
        // Print leading spaces
        for (int j = 0; j < n - i; j++) {
            printf("  "); // Two spaces for alignment
        }
        
        // Print stars
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }

        printf("\n"); // Move to next line
    }

    return 0;
}

Explanation:

  1. We declare an integer variable n and set it to 5 (number of rows).
  2. The outer loop (for (int i = n; i >= 1; i--)) runs from n to 1, controlling the rows.
  3. The first inner loop (for (int j = 0; j < n - i; j++)) prints spaces before the stars, ensuring right alignment.
  4. The second inner loop (for (int j = 1; j <= i; j++)) prints stars in each row.
  5. printf(" ") prints two spaces for alignment.
  6. printf("\n") moves to the next line after printing spaces and stars.

Output:

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

Conclusion

In this tutorial, we learned how to print an inverted right-angled triangle pattern in C:

  1. Basic star pattern: Used nested loops to print an inverted right-angled triangle.
  2. User input pattern: Allowed dynamic pattern printing based on user input.
  3. Number pattern: Printed numbers instead of stars in decreasing order.