C Example | Display Diamond Star Pattern

This c programming example code shows how to display diamond star pattern.

#include<stdio.h>
int main()
{
 	int i, j, k, rows;
 	printf("Enter Diamond Rows =  ");
 	scanf("%d", &rows);
    
    int x = rows - 1;
    int y = 1;

    printf("Diamond Star Pattern\n");
    for(i = 1; i <= rows; i++)
    {
        for(j = 1; j <= x; j++)
        {
            printf(" ");
        }
        for(k = 1; k <= y; k++)
        {
            printf("*");
        }
        if(x > i)
        {
            x = x - 1;
            y += 2;
        }
        if(x < i)
        {
            x += 1;
            y = y - 2;
        }
        printf("\n");
    }
 	return 0;
}

Run this code, we may get:

Enter Diamond Rows =  12
Diamond Star Pattern
           *
          ***
         *****
        *******
       *********
      ***********
      ***********
       *********
        *******
         *****
          ***
           *