Skip to content

C program to print Diamond Pattern

by yashan on September 12th, 2011

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

5 Comments
  1. very good and simly method is used in program so we are all greatful to yuo…

  2. Yuvraj permalink

    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
    }
    }

  3. Kiran S permalink

    It’s nice. Thank you.

  4. Vivek A permalink

    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

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS