The diamond pattern in C language: This code prints a diamond pattern of stars. The diamond shape is as follows:
* *** ***** *** *
Diamond pattern program in C
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 of program:
Download Diamond program.
C program to print diamond using recursion
void print (int);
int main () {
int rows;
scanf("%d", &rows);
print(rows);
return 0;
}
void print (int r) {
int c, space;
static int stars = -1;
if (r <= 0)
return;
space = r - 1;
stars += 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf("\n");
print(--r);
space = r + 1;
stars -= 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf("\n");
}