C Program to Print Diamond Pattern

Write a C Program to Print the Diamond Pattern of different kinds of stars, numbers, and alphabets using for loop, while loop, do while loop, and function. A diamond pattern is a combination of a triangle and an inverted triangle. So, we have to use one multi-level loop to create the top triangle and another for the inverted triangle.

C Program to Print Diamond Star Pattern

This C Diamond Star Pattern example uses two sets of for loops to print a diamond’s upper and lower portion. There are multi-level 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 program Example, we removed the multiple for loops and used if statements to print the 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. We just replaced the for loop with 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
          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
*********************
 *******************
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *

This example program prints the diamond pattern using the do while loop.

#include<stdio.h>
int main(void)
{
    int i, j, rows;
    printf("Enter Diamond Rows =  ");
    scanf("%d", &rows);
    
    i = 1;
    
    do  {
        j = 1;
        while(j <= rows - i)
        {
            printf(" ");
            j++;
        }
        j = 1;
        while(j <= i * 2 - 1)
        {
            printf("*");
            j++;
        }
        printf("\n");
        i++;
    }  while(i <= rows);
    
    i = rows - 1;
    do {
        j = 1;
        while(j <= rows - i)
        {
            printf(" ");
            j++;
        }
        j = 1;
        while(j <= i * 2 - 1)
        {
            printf("*");
            j++;
        }
        printf("\n");
        i--;
    } while(i > 0);
}

Output

Enter Diamond Rows =  12
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
   *****************
  *******************
 *********************
***********************
 *********************
  *******************
   *****************
    ***************
     *************
      ***********
       *********
        *******
         *****
          ***
           *

In all the above examples, the code inside the nested for loop is the same for the upper and lower portions of the diamond. So, if we use the functions in the C Program to Print Diamond Pattern of stars, we can utilize the code reusability. Apart from this, the below program accepts the symbol and prints the diamond pattern of the user-given symbol.

#include<stdio.h>
void result(int rows, int i, char ch){
    for(int j = 1; j <= rows - i; j++)
    {
        printf(" ");
    }
    for(int k = 1; k <= i * 2 - 1; k++)
    {
        printf("%c", ch);
    }
    printf("\n");
}

int main(void)
{
    int i, rows;
    char ch;
    
    printf("Enter Symbol =  ");
    scanf("%c", &ch);
    
    printf("Enter Diamond Rows =  ");
    scanf("%d", &rows);
    
    for(i = 1; i <= rows; i++)
    {
        result(rows, i, ch);
    }
    
    for(i = rows - 1; i > 0; i--)
    {
        result(rows, i, ch);
    }
}

Output.

Enter Symbol =  $
Enter Diamond Rows =  11
          $
         $$$
        $$$$$
       $$$$$$$
      $$$$$$$$$
     $$$$$$$$$$$
    $$$$$$$$$$$$$
   $$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$$$$
 $$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$
 $$$$$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$$$$
   $$$$$$$$$$$$$$$
    $$$$$$$$$$$$$
     $$$$$$$$$$$
      $$$$$$$$$
       $$$$$$$
        $$$$$
         $$$
          $

The below-mentioned programs are the remaining programs that display the diamond pattern. However, we have used the for loop for all the examples, but you can use the hyperlinks to see the other looping techniques.

Hollow Diamond Pattern of stars

The below example prints the Hollow Diamond Pattern of stars using the for loop. For more examples >> Click Here.

#include<stdio.h>
void loopLogic(int rows, int i)
{
    for (int j = 1; j <= rows - i; j++) {
        printf(" ");
    }
    for (int k = 1; k <= i * 2 - 1; k++) {
        if (k == 1 || k == i * 2 - 1) {
            printf("*");
        } else {
            printf(" ");
        }
    }
    printf("\n");
}
int main(void) {
    int i, rows;
    
    printf("Enter Hollow Diamond Pattern Rows =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++) {
        loopLogic(rows, i);
    }
    for (i = rows - 1; i > 0; i--) {
        loopLogic(rows, i);
    }
}

Output

Enter Hollow Diamond Pattern Rows =  9
        *
       * *
      *   *
     *     *
    *       *
   *         *
  *           *
 *             *
*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *

Half Diamond Star Pattern

The below C program example prints the Half Diamond Pattern of stars using the for loop and functions. For more examples >> Click Here.

#include<stdio.h>
void loopLogic(int rows, int i)
{
    for (int j = 1; j <= i; j++) {
        printf("*");
    }
    printf("\n");
}
int main(void) {
    int i, rows;
    
    printf("Enter Half Diamond Pattern Rows =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++) {
        loopLogic(rows, i);
    }
    for (i = rows - 1; i > 0; i--) {
        loopLogic(rows, i);
    }
}

Output

Enter Half Diamond Pattern Rows =  10
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*

Half Mirrored Diamond Star Pattern

The below example prints the Mirrored Half Diamond Star Pattern using the for loop. For more examples >> Click Here.

#include<stdio.h>
void loopLogic(int rows, int i)
{
    for (int j = 1; j <= rows - i; j++) {
        printf(" ");
    }
    for (int k = 1; k <= i; k++) {
        printf("*");
    }
    printf("\n");
}
int main(void) {
    int i, rows;
    
    printf("Enter Mirrored Half Diamond Pattern Rows =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++) {
        loopLogic(rows, i);
    }
    for (i = rows - 1; i > 0; i--) {
        loopLogic(rows, i);
    }
}

Output

Enter Mirrored Half Diamond Pattern Rows =  9
        *
       **
      ***
     ****
    *****
   ******
  *******
 ********
*********
 ********
  *******
   ******
    *****
     ****
      ***
       **
        *
        *

Hollow Diamond inside a Square Pattern

The below example prints the Hollow Diamond inside a Square Pattern using the for loop. For more examples >> Click Here.

#include<stdio.h>
int main(void)
{
    int i, j, k, rows;
    
    printf("Enter Hollow Diamond inside Square Rows =  ");
    scanf("%d", &rows);
    
    for (i = 0 ; i < rows; i++ )
    {
        for (j = 0 ; j < rows; j++ )
        {
            if(j < rows - i) {
                printf("*");
            }
            else {
                printf(" ");
            }
        }
        for (k = 0 ; k < rows; k++ )
        {
            if (k < i ) {
                printf(" ");
            }
            else {
                printf("*");
            }
        }
        printf("\n");
    }
    
    for (i = 1 ; i <= rows; i++ )
    {
        for (j = 0 ; j < rows; j++ )
        {
            if(j < i) {
                printf("*");
            }
            else {
                printf(" ");
            }
        }
        for (k = 0 ; k < rows; k++ )
        {
            if (k < rows - i ) {
                printf(" ");
            }
            else {
                printf("*");
            }
        }
        printf("\n");
    }
}

Output

Enter Hollow Diamond inside Square Rows =  8
****************
*******  *******
******    ******
*****      *****
****        ****
***          ***
**            **
*              *
*              *
**            **
***          ***
****        ****
*****      *****
******    ******
*******  *******
****************

C Program to Print Diamond Pattern of Numbers

All the above-shown examples display Dimond patterns of stars in different shapes; however, this section shows how to display the diamond pattern with numbers.

The below example prints the Diamond Pattern of numbers using the for loop. For more examples >> Click Here.

#include<stdio.h>
void result(int rows, int i){
    for (int j = 1; j <= rows - i; j++) {
        printf(" ");
    }
    for (int k = 1; k <= i * 2 - 1; k++) {
        printf("%d", k);
    }
    printf("\n");
}
int main(void) {
    int i, rows;
    
    printf("Enter Diamond Number Pattern Rows =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++) {
        result(rows, i);
    }
    for (i = rows - 1; i > 0; i--) {
        result(rows, i);
    }
}

Output

Enter Diamond Number Pattern Rows =  4
   1
  123
 12345
1234567
 12345
  123
   1

Hollow Diamond Number Pattern

This example shows the way to print the hollow diamond pattern of numbers.

#include<stdio.h>
void result(int rows, int i){
    for (int j = 1; j <= rows - i; j++) {
        printf(" ");
    }
    for (int k = 1; k <= i * 2 - 1; k++) {
        if (k == 1 || k == i * 2 - 1) {
            printf("%d", k);
        } else {
            printf(" ");
        }
    }
    printf("\n");
}
int main(void) {
    int i, rows;
    
    printf("Enter Hollow Diamond Number Pattern Rows =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++) {
        result(rows, i);
    }
    for (i = rows - 1; i > 0; i--) {
        result(rows, i);
    }
}

Output

Enter Hollow Diamond Number Pattern Rows =  7
      1
     1 3
    1   5
   1     7
  1       9
 1         11
1           13
 1         11
  1       9
   1     7
    1   5
     1 3
      1

C Program to Print Diamond Pattern of Alphabets

This example uses the for loop to print the diamond pattern of alphabets starting from A to the remaining characters. For more examples >> Click Here.

#include<stdio.h>
void result(int rows, int i)
{
    int alphabet = 64;
    for (int j = 1 ; j <= rows - i; j++ )
    {
        printf(" ");
    }
    for (int k = 1 ; k <= i * 2 - 1; k++ )
    {
        printf("%c", alphabet + k);
    }
    printf("\n");
}
#include <stdio.h>

int main(void)
{
    int i, rows;

    printf("Enter Diamond Pattern of Alphabets Rows = ");
    scanf("%d", &rows);
        
    for (i = 1 ; i <= rows; i++ )
    {
        result(rows, i);
    }
        
    for (i = rows - 1 ; i > 0; i-- )
    {
        result(rows, i);
    }
}

Output

Enter Diamond Pattern of Alphabets Rows = 9
        A
       ABC
      ABCDE
     ABCDEFG
    ABCDEFGHI
   ABCDEFGHIJK
  ABCDEFGHIJKLM
 ABCDEFGHIJKLMNO
ABCDEFGHIJKLMNOPQ
 ABCDEFGHIJKLMNO
  ABCDEFGHIJKLM
   ABCDEFGHIJK
    ABCDEFGHI
     ABCDEFG
      ABCDE
       ABC
        A

Hollow Diamond Alphabets Pattern

This example prints the hollow diamond pattern of alphabets.

#include<stdio.h>
void result(int rows, int i)
{
    int alphabet = 64;
    for (int j = 1 ; j <= rows - i; j++ )
    {
        printf(" ");
    }
    for (int k = 1 ; k <= i * 2 - 1; k++ )
    {
        if (k == 1 || k == i * 2 - 1) {
            printf("%c", alphabet + k);
        } else {
            printf(" ");
        }
    }
    printf("\n");
    
}
#include <stdio.h>

int main(void)
{
    int i, rows;
    
    printf("Enter Diamond Pattern of Alphabets Rows = ");
    scanf("%d", &rows);
    
    for (i = 1 ; i <= rows; i++ )
    {
        result(rows, i);
    }
    
    for (i = rows - 1 ; i > 0; i-- )
    {
        result(rows, i);
    }
}

Output

Enter Diamond Pattern of Alphabets Rows = 8
       A
      A C
     A   E
    A     G
   A       I
  A         K
 A           M
A             O
 A           M
  A         K
   A       I
    A     G
     A   E
      A C
       A