C program to print hollow pyramid star pattern

  • Write a C program to print hollow pyramid star pattern.

Hollow pyramid star pattern program’s output should be:

Hollow_Pyramid_Pattern

Required Knowledge

Algorithm to print hollow pyramid star pattern using for loop

This program is similar to pyramid star pattern. The only difference is, from first to second last row we will only print first and last star character of any row and we will replace all internal star characters by space character. Then we will print 2*N-1 (N = number of rows in pattern) star characters in last row.

Here is the matrix representation of the hollow pyramid star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Hollow_Pyramid_Star_Pattern

C program to print hollow pyramid star pattern

C program to print hollow pyramid star pattern

#include<stdio.h>
 
int main() {
    int i, space, rows, star=0;
    printf("Enter the number of rows\n");
    scanf("%d",&rows);
 
    /* printing one row in every iteration */
    for(i = 0; i < rows-1; i++) {
        /* Printing spaces */
        for(space = 1; space < rows-i; space++) {
            printf(" ");
        }
        /* Printing stars */
        for (star = 0; star <= 2*i; star++) {
            if(star==0 || star==2*i)
                printf("*");
            else
                printf(" ");
        }
        /* move to next row */
        printf("\n");
    }
    /* print last row */
    for(i=0; i<2*rows-1; i++){
        printf("*");
    }
    return 0;
}

Output

Enter the number of rows
6
     *
    * *
   *   *
  *     *
 *       *
***********