C Program To Print Diamond Pattern

Problem:

Write a c program to print or display diamond pattern.

Algorithm or Solution:

  1. Start
  2. Accept number of rows as input from the user.
  3. Display " " (space) and "*" (star) using for loops which will make the diamond pattern in stars.
  4. End

C Program / Source Code:

Here is the source code of c program to print or the display diamond pattern. This code prints the diamond pattern of stars.
/* Aim: Write a C program to print or display diamond pattern. */

#include<stdio.h>

int main()
{
 int i,n,j,s=1;

 printf("\n Enter number of rows:- ");
 scanf("%d",&n);
 printf("\n This is the diamond pattern in stars: \n");

 s=n-1;

 for(j=1;j<=n;j++)
 {
  for(i=1;i<=s;i++)
   printf(" ");

  s--;

  for(i=1;i<=2*j-1;i++)
   printf("*");

  printf("\n");
 }

 s=1;

 for(j=1;j<=n-1;j++)
 {
  for(i=1;i<=s;i++)
   printf(" ");

  s++;

  for(i=1;i<=2*(n-j)-1;i++)
   printf("*");

  printf("\n");
 }

 return 0;
}

/* Output of above code:-

 Enter number of rows:- 5

 This is the diamond pattern in stars:

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

*/