C Program to Print Diamond Pattern

C Program to Print Diamond Pattern:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include <stdio.h>
 main()
{
    int num,i,k,cnt=1;

    printf("Enter number of Rows\n");
    scanf("%d",&num);
    cnt=num-1;
    for(k=1;k<=num;k++)
    {
        for(i=1;i<=cnt;i++)
            printf(" ");
        cnt--;
        for(i=1;i<=2*k-1;i++)
            printf("*");
        printf("\n");
     }
     cnt=1;
     for(k=1;k<=num-1;k++)
     {
         for(i=1;i<=cnt;i++)
             printf(" ");
         cnt++;
         for(i=1;i<=2*(num-k)-1;i++)
             printf("*");
         printf("\n");
      }
}

OUTPUT:

Enter number of rows
6
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.