C Program to Print Diamond Pattern

Write a C Program to Print Diamond Pattern.

Example,

Input
5

Output
  *
 ***
*****
 ***
  *

Input
6

Output
  *
 ***
*****
*****
 ***
  *

Explanation

  1. First, we print the Upper Triangle.
  2. The Upper Triangle consists of N/2 + 1 rows (if N is odd) or N/2 rows (if N is even).
  3. The number of ‘*’ in the first row is 1 and it increases by 2 as we move down the rows.
  4. If i is the number of ‘*’ we need to print in the current row. Then the number of spaces before ‘*’ is ( N – i ) / 2.
  5. Print (N – i)/2 spaces then Print i ‘*’ characters for each row.
  6. After printing all rows of the Upper Triangle, we move to the lower part.
  7. The Lower Triangle consists of N/2 rows.
  8. The first row of Lower Triangle consists of N/2 + N/2 – 1 ‘*’ characters.
    Reason: N/2 + N/2 – 1 gives the closest odd number to N that is less than N.
    For example, if N is 6 then N/2 + N/2 – 1 = 5.
    If N is 5, then N/2 + N/2 – 1 = 3.
  9. The number of ‘*’ decreases by 2 as we move down the rows.
  10. If i is the number of ‘*’ we need to print in the current row. Then the number of spaces before ‘*’ is ( N – i ) / 2.
  11. Print (N – i)/2 spaces then Print i ‘*’ characters for each row.

The Program can be implemented in 2 ways

  1. Diamond Pattern Program using a loop (iteratively)
  2. Diamond Pattern Program using recursion (recursively)

Program to Print Diamond Pattern in C using Loop

#include<stdio.h>

int main() {

	int n, i, k;
	printf("Enter Number: ");
	scanf("%d", &n);

	// Printing the upper part of Diamond Pattern
	// i is the number of stars we print in
	// each iteration of loop
	for (int i = 1; i <= n; i = i + 2)
	{
		// printing (n-i)/2 spaces
		for (k = 1; k <= (n - i) / 2; ++k)
		{
			printf(" ");
		}

		// printing i '*"
		for (k = 1; k <= i; ++k)
		{
			printf("*");
		}

		printf("\n");
	}

	// Printing Lower part of Diamond Pattern
	// n/2+n/2-1 converts integer n to nearest odd number lower than n
	// this is done because the number of stars in each row is odd
	// i is the number of stars we print in
	// each iteration of loop
	for (i = n / 2 + n / 2 - 1; i >= 1; i = i - 2)
	{
		// printing (n-i)/2 spaces
		for (k = 1; k <= (n - i) / 2; ++k)
		{
			printf(" ");
		}

		// printing i '*"
		for (k = 1; k <= i; ++k)
		{
			printf("*");
		}

		printf("\n");
	}

	return 0;

}

Output

Program to Print Diamond Pattern in C using recursion

#include<stdio.h>

// i is the number of '*' we need to print
// del is used to increment/decrement i
// for next recursive call
void print(int n, int i, int del) {

	int k;

	if (i<1 || i>n)
	{
		return;
	}

	// printing (n-i)/2 spaces
	for (k = 1; k <= (n - i) / 2; ++k)
	{
		printf(" ");
	}

	// printing i '*"
	for (k = 1; k <= i; ++k)
	{
		printf("*");
	}

	printf("\n");

	print(n, i + del, del);

}

int main() {

	int n, i, k;
	printf("Enter Number: ");
	scanf("%d", &n);

	// Printing Upper Triangle
	print(n, 1, 2);

	// Printing Lower Triangle
	// n/2 + n/2 - 1 convert n to closest odd number
	// which is lesser than n
	print(n, n / 2 + n / 2 - 1, -2);

	return 0;

}

Output

C diamond pattern program output

What to study next?

  1. Reverse Array in C
  2. Program To Left-Shift Array by 1
  3. Factorial Program in C

Leave Comment if you face any problem.

Leave a Comment

Your email address will not be published. Required fields are marked *