Hollow Diamond Pattern In C - C Programming Tutorial
C Course / Programs / Hollow Diamond Pattern In C

Hollow Diamond Pattern In C

BLUF: Understanding Hollow Diamond Pattern In C is a foundational part of learning C programming. This tutorial explains the core principles and syntax needed to implement this concept effectively.
Core Programming Principle: Hollow Diamond Pattern In C

C provides direct access to memory and system resources. Learn how Hollow Diamond Pattern In C leverages this power in the lesson below.

Star pattern in C with hollow diamonds

Let's develop a C program that generates a pattern of hollow diamond shapes:

Example

#include<stdio.h>
int main()
{
  int n;

  printf("Enter number of rows: ");
  scanf("%d",&n);

  for(int i=1; i<=n; i++)
  {
     for(int j=i; j<=n; j++)
     {
       printf(" ");
     }
     for(int k=1; k<=2*i-1; k++)
     {
       if(k==1 || k==(2*i-1)) printf("*");
       else printf(" ");
     }
     printf("\n");
  }

  for(int i=n-1; i>=1; i--)
  {
     for(int j=n; j>=i; j--)
     {
       printf(" ");
     }
     for(int k=1; k<=2*i-1; k++)
     {
       if(k==1 || k==2*i-1) printf("*");
       else printf(" ");
     }
     printf("\n");
  }

  return 0;
}

Output:

Output

Enter number of rows: 7

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

Numbering Pattern in a Hollow Diamond:

Instead of asterisks, this outline diamond pattern contains numerical values. Consequently, only a fraction of the code will be modified, leaving the majority unaffected.

The pattern counts up from 1 to 5 starting with 1 . Only 1 appears in the first and last rows, whereas exactly 2 identical numbers appear in all other rows. The size of the design increases from the first to the fifth row while decreasing from the sixth to the ninth row.

Example

#include<stdio.h>
int main()
{
   int n, a=1;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i=1; i<=n; i++)
   {
     for(int j=i; j<=n; j++)
     {
       printf(" ");
     }
     for(int k=1; k<=2*i-1; k++)
     {
       if(k==1 || k==2*i-1) printf("%d",a);
       else printf(" ");
     }
     a++;
     printf("\n");
   }

   a=n-1;

   for(int i=n-1; i>=1; i--)
   {
     for(int j=n; j>=i; j--)
     {
       printf(" ");
     }
     for(int k=1; k<=2*i-1; k++)
     {
       if(k==1 || k==2*i-1) printf("%d",a);
       else printf(" ");
     }
     a--;
     printf("\n");
   }

   return 0;
}

Output:

Output

Input: 5

     1
    2 2
   3   3
  4     4
 5       5
  4     4
   3   3
    2 2
     1

In this pattern, the initial and final rows contain a single character, while all the middle rows consist of exactly two characters. Just substitute the * with a different symbol to showcase any character instead of the stars. However, for this exercise, ensure that each symbol used is distinct.

We will utilize the subsequent 2 techniques to display the empty diamond shape in C:

  • Implementing a for loop
  • Utilizing a while loop
  • First approach: Using for Loop

Example

// The Hollow Diamond Printing C Program
// using a for loop in a pattern
#include <stdio.h>
int main()
{

	int n = 5, rows, columns;

	// For loop is utilised to recognise
	// upper triangle is printed using the amount of rows.
	for (rows = 1; rows <= n; rows++) {

		// used for printing the spaces
		for (columns = n; columns > rows; columns--) {
			printf(" ");
		}

		// print star
		printf("*");

		// again print the spaces
		for (columns = 1; columns < (rows - 1) * 2;
			columns++) {
			printf(" ");
		}
		if (rows == 1) {
			printf("\n");
		}
		else {
			printf("*\n");
		}
	}
// For loop is utilised to recognise
	// lower triangle is printed using the amount of rows.
	for (rows = n - 1; rows >= 1; rows--) {

		// used for printing the spaces
		for (columns = n; columns > rows; columns--) {
			printf(" ");
		}

		// print star
		printf("*");
		for (columns = 1; columns < (rows - 1) * 2;
			columns++) {
			printf(" ");
		}
		if (rows == 1) {
			printf("\n");
		}
		else {
			printf("*\n");
		}
	}
	return 0;
}

Output:

Output

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

Another approach: Using While Loop

Now, we are able to display the empty diamond shape by utilizing a while loop:

Example

// The Hollow Diamond Printing C Program
// using a while loop in a pattern
#include <stdio.h>

int main()
{

	int n = 5, rows = 1, columns;
	// Identifying is done using a while loop.
	// the quantity of rows and
	// The upper triangle is printed using it.
	while (rows <= n) {
		columns = n;

		// the method for printing spaces
		while (columns > rows) {
			printf(" ");
			columns--;
		}

		// star in print
		printf("*");
		columns = 1;
		while (columns < (rows - 1) * 2) {
			printf(" ");
			columns++;
		}
		if (rows == 1) {
			printf("\n");
		}
		else {
			printf("*\n");
		}
		rows++;
	}
	// It uses a while loop to identify
	// the quantity of rows and
	// it is used to print lower triangle
	rows = n - 1;
	while (rows >= 1) {
		columns = n;
		// used for printing the spaces
		while (columns > rows) {
			printf(" ");
			columns--;
		}
		// print star
		printf("*");
		columns = 1;
		while (columns < (rows - 1) * 2) {
			printf(" ");
			columns++;
		}
		if (rows == 1) {
			printf("\n");
		}
		else {
			printf("*\n");
		}
		rows--;
	}
	return 0;
}

Output:

Output

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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience