C program to print diamond star pattern

  • Write a program in C to print diamond star pattern using for loop.
  • How to print a diamond shape pattern of using loops in C.

Diamond star pattern program’s output should be:

Diamond_Star_Pattern

Required Knowledge

Algorithm to print diamond star pattern using loop
Diamond star pattern is a combination of pyramid star pattern and inverse pyramid star pattern. This program is combination of both, it first prints a pyramid followed by a reverse pyramid star pattern.

C program to print diamond star pattern

C program to print 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)) {
            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)) {
            printf("*");
            star++;
        }
        printf("\n");
    }
 
    return 0;
}

Output

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