Print an Inverted Pyramid Star Pattern in C

To print an inverted pyramid star pattern in C, we use nested loops where the outer loop controls the number of rows and the inner loop prints decreasing stars in each row.


Examples to Print an Inverted Pyramid Star Pattern

1. Basic Inverted Pyramid Star Pattern

In this example, we print an inverted pyramid pattern where the number of stars in each row decreases from top to bottom.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5; 

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

    return 0;
}

Explanation:

  1. The variable rows is initialized to 5, representing the number of rows in the pattern.
  2. The outer loop (for (int i = rows; i >= 1; i--)) runs from 5 to 1, decreasing by 1 in each iteration. Refer For Loop in C.
  3. The inner loop (for (int j = 1; j <= i; j++)) prints stars in each row, decreasing as i decreases.
  4. The printf("\n") statement moves to the next line after printing the stars for the current row.

Output:

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

2. Inverted Pyramid with Center Alignment

In this example, we print an inverted pyramid pattern with spaces to center-align the stars.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5;

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

    return 0;
}

Explanation:

  1. The variable rows is set to 5, defining the number of rows in the pattern.
  2. The first for loop (for (int space = 0; space < rows - i; space++)) prints leading spaces to center-align the stars.
  3. The second for loop (for (int j = 1; j <= (2 * i - 1); j++)) prints stars, decreasing in count with each row.
  4. The printf("\n") statement moves to the next line after printing stars and spaces for the current row.

Output:

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

3. Hollow Inverted Pyramid Star Pattern

In this example, we print a hollow inverted pyramid where only the boundary stars are printed.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5;

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

    return 0;
}

Explanation:

  1. The variable rows is set to 5, representing the number of rows.
  2. The first for loop controls the rows, starting from 5 and decreasing.
  3. The second for loop (for (int j = 1; j <= (2 * i - 1); j++)) controls printing of stars and spaces.
  4. An if-else statement checks whether it’s the first row, first star, or last star in the row; if so, it prints a star, otherwise, it prints a space.

Output:

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

Conclusion

In this tutorial, we explored different ways to print an inverted pyramid star pattern in C:

  1. Basic Inverted Pyramid: A decreasing star pattern using nested loops.
  2. Center-Aligned Pyramid: Added spaces to center-align the stars.
  3. Hollow Inverted Pyramid: Printing only boundary stars while keeping the inside hollow.