Complete Diamond Pattern with Decreasing Values in C

What You’ll Learn
How to print a complete symmetric diamond-style number pattern where values decrease from the edges (k) to the center (1) and then increase back. We’ll build the upper and lower halves separately to keep the output symmetric without duplicating the center row.
⭐ Pattern Output
For k = 5, the pattern looks like this:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5Complete C Program
The first part prints i = k..1. The second part prints i = 2..k to mirror the top and avoid repeating the center row.
#include <stdio.h>
int main() {
int i, j;
int k = 5;
for (i = k; i >= 1; i--) {
for (j = k; j >= 1; j--) {
if (j > i)
printf("%d ", j);
else
printf("%d ", i);
}
for (j = 2; j <= k; j++) {
if (j > i)
printf("%d ", j);
else
printf("%d ", i);
}
printf("\n");
}
for (i = 2; i <= k; i++) {
for (j = k; j >= 1; j--) {
if (j > i)
printf("%d ", j);
else
printf("%d ", i);
}
for (j = 2; j <= k; j++) {
if (j > i)
printf("%d ", j);
else
printf("%d ", i);
}
printf("\n");
}
return 0;
}🧠 How It Works
Set k (outer value)
k is the maximum edge value and controls size.
Upper half: i = k..1
This part moves toward the center value (1).
Symmetry per row
Each row is made symmetric using two inner loops: left half (j=k..1) and right half (j=2..k).
Choose j or i
If j > i, print j (outer). Otherwise print i (inner).
Lower half: i = 2..k
Start at 2 to avoid printing the center row twice, then mirror the top.
Complete decreasing diamond
Values decrease to the center, then increase back due to the two-part loop structure.
Variation — User Input Version
Read k from the user to print different sizes.
#include <stdio.h>
int main() {
int i, j;
int k;
printf("Enter the maximum value (pattern size): ");
scanf("%d", &k);
for (i = k; i >= 1; i--) {
for (j = k; j >= 1; j--) {
if (j > i)
printf("%d ", j);
else
printf("%d ", i);
}
for (j = 2; j <= k; j++) {
if (j > i)
printf("%d ", j);
else
printf("%d ", i);
}
printf("\n");
}
for (i = 2; i <= k; i++) {
for (j = k; j >= 1; j--) {
if (j > i)
printf("%d ", j);
else
printf("%d ", i);
}
for (j = 2; j <= k; j++) {
if (j > i)
printf("%d ", j);
else
printf("%d ", i);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Let the user choose
k(shown above) - Remove trailing spaces by adjusting printing logic
- Reverse the comparison to make values increase toward the center
- Print only the border values for a hollow variant
Avoid
- Starting the lower half at
i = 1(would duplicate the center row) - Using
k < 1without validating input
Key Takeaways
A complete diamond is built using an upper half and a lower half.
Each row is mirrored using two inner loops (left and right halves).
The condition j > i controls whether an outer or inner value is printed.
The runtime grows roughly as O(k²).
❓ Frequently Asked Questions
i=1 is already printed as the center row in the upper half. Starting from 2 avoids duplication.2k - 1 rows. For k=5, that’s 9 rows.printf("%d ", ...) or print spaces conditionally.(2k-1) × (2k-1).Explore More C Number Patterns!
Keep practicing nested loops and conditions with new patterns.
A full pattern of size k has 2k-1 rows and 2k-1 values per row. For k=5, that’s a 9×9 grid.
12 people found this page helpful
