C program to print hollow diamond star pattern

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

Hollow Diamond star pattern program’s output should be:

Hollow_Diamond_Star_Pattern

Required Knowledge

  • For Loop
  • printf and scanf function in C

Algorithm to print hollow diamond star pattern using for loop

This program is similar to diamond star pattern. The only difference is, here we will only print first and last star character of any row and we will replace all internal star characters by space character.

C program to print hollow or empty diamond star pattern

C program to print hollow diamond star pattern

#include<stdio.h>
 
int main() {
    int i, space, rows=7, star=0;
     
    /* Printing upper triangle */
    for(i = 1; i <= rows; i++) {
        /* Printing spaces */
        for(space = 1; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        while(star != (2*i - 1)) {
         if(star == 0 or star==2*i-2)
                printf("*");
            else
                printf(" ");
            star++;
        }
        star=0;
        /* move to next row */
        printf("\n");
    }
    rows--;
    /* Printing lower triangle */
    for(i = rows;i >= 1; i--) {
        /* Printing spaces */
        for(space = 0; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        star = 0;
        while(star != (2*i - 1)) {
         if(star == 0 or star==2*i-2)
                printf("*");
            else
                printf(" ");
            star++;
        }
        printf("\n");
    }
 
    return 0;
}

Output

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