Diamond Number Pattern in C

What You’ll Learn
How to print a diamond number pattern in C using nested loops. The upper half grows from 1 digit to 9 digits, then the lower half shrinks back to 1.
You’ll also see how to use leading spaces to center-align each row, and how 2 * i - 1 controls the width of the diamond.
⭐ Pattern Output
For rows = 5 (upper half), the pattern looks like this:
1
123
12345
1234567
123456789
1234567
12345
123
1Complete C Program
This version prints the upper half for i = 1..rows, then prints the lower half for i = rows-1..1.
#include <stdio.h>
int main() {
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for (j = 1, k = 1; j <= 2 * i - 1; j++, k++) {
printf("%d", k);
}
printf("\n");
}
for (i = rows - 1; i >= 1; i--) {
for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for (j = 1, k = 1; j <= 2 * i - 1; j++, k++) {
printf("%d", k);
}
printf("\n");
}
return 0;
}🧠 How It Works
Set the row count
int rows = 5; controls the height of the upper half (and thus the full diamond).
Upper half (growing rows)
for (i = 1; i <= rows; i++) builds the top by increasing the row width each time.
Center alignment (spaces)
rows - i leading spaces center each row so the shape looks like a diamond.
Row width (odd counts)
2 * i - 1 produces widths 1, 3, 5, 7, 9… and k prints 1..(2*i-1).
Lower half (shrinking rows)
for (i = rows - 1; i >= 1; i--) mirrors the upper half to complete the diamond.
Diamond number pattern
Each row prints an odd number of digits, so the work grows roughly like O(n²) with n rows.
Variation — User Input Version
Accept the upper-half row count at runtime using scanf():
#include <stdio.h>
int main() {
int rows;
int i, j, k;
printf("Enter the number of rows (upper half): ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for (j = 1, k = 1; j <= 2 * i - 1; j++, k++) {
printf("%d", k);
}
printf("\n");
}
for (i = rows - 1; i >= 1; i--) {
for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for (j = 1, k = 1; j <= 2 * i - 1; j++, k++) {
printf("%d", k);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Allow
rowsinput usingscanf()(shown above) - Add spaces between digits with
printf("%d ", k) - Print a hollow diamond (only border digits)
- Replace digits with letters for an alphabet diamond
Avoid
- Forgetting leading spaces (diamond becomes left-aligned)
- Using
rows <= 0without validating input - Printing an extra newline inside inner loops
Key Takeaways
The diamond is made from an upper half (growing) and a lower half (shrinking).
Leading spaces (rows - i) keep the output center-aligned.
The row width is controlled by 2 * i - 1 (odd numbers).
This same idea applies to diamond star patterns too.
❓ Frequently Asked Questions
i prints 2*i-1 digits: 1, 3, 5, 7, 9…rows (upper half). For example, rows = 6 will reach width 11 on the middle row.Explore More C Number Patterns!
Practice loops with triangles, pyramids, diamonds, and more.
In the example shown (rows = 5), the total digits printed are 1+3+5+7+9+7+5+3+1 = 41. This is why the overall work grows roughly like O(n²) as rows increases.
12 people found this page helpful
