Q:

C program to print diamond pattern

0

C program to print diamond pattern

 

The diamond pattern in C language: This code prints a diamond pattern of stars. The diamond shape is as follows:

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include 
int main()
{
  int n, c, k;

  printf("Enter number of rows\n");
  scanf("%d", &n);

  for (k = 1; k <= n; k++)
  {
    for (c = 1; c <= n-k; c++)
      printf(" ");

    for (c = 1; c <= 2*k-1; c++)
      printf("*");

    printf("\n");
  }

  for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= k; c++)
      printf(" ");

    for (c = 1 ; c <= 2*(n-k)-1; c++)
      printf("*");

    printf("\n");
  }

  return 0;
}

output:

Enter number of rows

10

         *

        ***

       *****

      *******

     *********

    ***********

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

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

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

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

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

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

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

    ***********

     *********

      *******

       *****

        ***

         *

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now