C program to print Diamond Pattern

Write a program in C programming language to print the diamond pattern as the output of the C program. Below is the code of the C program –

/* C program to print diamond as output*/
#include<stdio.h>
#include<conio.h>
main()
{
      int n,i,j,k;
      printf("enter number of rows in odd number");
      scanf("%d",&n);
      for(i=0;i<=(n/2);i++)
      {
              for(j=0;j<(n/2)-i;j++)
              {
                      printf(" ");
              }
              for(k=0;k<(2*i)+1;k++)
              {
                      printf("*");
              }
              printf("\n");
      }

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

From the above code of the C program you will get the output as follows –

Diamond in C program

Diamond output of C program

yashan has written 69 articles

5 thoughts on “C program to print Diamond Pattern

  1. Yuvraj says:

    Hey, It is not the exact logic for a given program.
    Here i m giving the code with minimum number of executing steps.
    also i m sure that there doesn’t exist any other way to print the same Diamond such that code executing steps will be less than mine.
    It is not copied from any website, book etc.

    #include
    void main()
    {
    int n,i,s,k,flagReverce=0;
    printf(“Enter the no. of rows\n”);
    scanf(“%d”, &n);
    for (i=1; i>=1; i++)
    {
    for (s=n; s>i; s–)
    printf(” “);

    for(k=1; k<(i*2)-1; k++)
    printf("*")
    printf("\n")
    if (i == n)
    flagReverce = 1

    if (flagReverce = 1)
    i-=2
    }
    }

  2. Vivek A says:

    Hi Yuvraj,

    I did see your code which is very fine than other loopy huge programs and here is mine.

    #include

    #define NOR 5

    int main()
    {
    char ucRow = 0;
    char cStr = 0;
    char cSpace = 0;

    for(ucRow = -1 * (2*NOR – 2); ucRow <= (2*NOR – 2); ucRow+=2)
    {
    printf("\n");

    for(cSpace = 0; cSpace < abs(ucRow) / 2; cSpace++)
    printf(" ");

    for(cStr = 0; cStr < ((2*NOR – 1) – abs(ucRow)); cStr++)
    printf("*");
    }

    return !printf("\n");
    }

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>