C Program to Print Diamond Star Pattern

Write a C Program to Print Diamond Star Pattern using for loop. This c Diamond Star Pattern example uses two sets of for loops to print the upper and lower portion of a diamond. There are nested for loops to iterate rows and columns and print the diamond star pattern.

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

    printf("Diamond Star Pattern\n");
	for(i = 1; i <= rows; i++)
	{
		for(j = 1; j <= rows - i; j++)
		{
			printf(" ");
		}
        for(j = 1; j <= i * 2 - 1; j++)
        {
            printf("*");
        }
		printf("\n");
	}

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

 	return 0;
}
C Program to Print Diamond Star Pattern 1

In this C Program, we removed the multiple for loops and used if statements to print 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;
}
Enter Diamond Rows =  12
Diamond Star Pattern
           *
          ***
         *****
        *******
       *********
      ***********
      ***********
       *********
        *******
         *****
          ***
           *

This C program allows entering a symbol and prints that symbol in a Diamond pattern using a while loop.

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

    printf("Diamond Star Pattern\n");
	i = 1;
	while(i <= rows)
	{
		j = 1;
		while(j <= rows - i)
		{
			printf(" ");
			j++;
		}
		j = 1;
        while(j <= i * 2 - 1)
        {
            printf("*");
			j++;
        }
		printf("\n");
		i++;
	}
	i = rows - 1;
    while(i > 0)
	{
		j = 1;
		while(j <= rows - i)
		{
			printf(" ");
			j++;
		}
		j = 1;
        while(j <= i * 2 - 1)
        {
            printf("*");
			j++;
        }
		printf("\n");
		i--;
	}

 	return 0;
}
Enter Diamond Rows =  11
Diamond Star Pattern
          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
*********************
 *******************
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.