The Diamond Pattern is a symmetrical shape where the number of characters increases up to the centre and then decreases forming a diamond-like structure. It can be visualized as a full pyramid and an inverted full pyramid joined by their bases. In this article, we will learn how to print the Diamond Pattern using C program.

Program To Print Diamond Pattern
#include <stdio.h>
int main() {
int n = 5;
// First outer loop to iterator through each row
for (int i = 0; i < 2 * n - 1; i++) {
// Assigning values to the comparator according to
// the row number
int comp;
if (i < n) comp = 2 * (n - i) - 1;
else comp = 2 * (i - n + 1) + 1;
// First inner loop to print leading whitespaces
for (int j = 0; j < comp; j++)
printf(" ");
// Second inner loop to print stars *
for (int k = 0; k < 2 * n - comp; k++) {
printf("* ");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int n = 5;
// First outer loop to iterator through each row
for (int i = 0; i < 2 * n - 1; i++) {
// Assigning values to the comparator according to
// the row number
int comp;
if (i < n) comp = 2 * (n - i) - 1;
else comp = 2 * (i - n + 1) + 1;
// First inner loop to print leading whitespaces
for (int j = 0; j < comp; j++)
printf(" ");
// Second inner loop to print stars *
for (int k = 0; k < 2 * n - comp; k++) {
printf("%d ", k + 1);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int n = 5;
// First outer loop to iterator through each row
for (int i = 0; i < 2 * n - 1; i++) {
// Assigning values to the comparator according to
// the row number
int comp;
if (i < n) comp = 2 * (n - i) - 1;
else comp = 2 * (i - n + 1) + 1;
// First inner loop to print leading whitespaces
for (int j = 0; j < comp; j++)
printf(" ");
// Second inner loop to print stars *
for (int k = 0; k < 2 * n - comp; k++) {
printf("%c ", k + 'A');
}
printf("\n");
}
return 0;
}
Output
* | 1 | A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A
Explanation:
- Outer loop iterates through each row from 0 to 2 * n - 2 (as total 2 * n - 1 rows).
- comp determines the number of leading spaces based on the current row number.
- For the first half (i < n): comp = 2 * (n - i) - 1.
- For the second half (i >= n): comp = 2 * (i - n + 1) + 1.
- First inner loop prints leading white spaces in each row.
- Second inner loop iterates through the columns in the row to print * star.
- After completing a row, printf("\n") moves to the next line.