Diamond Number Pattern with Decreasing Values in C

What You’ll Learn
How to print a symmetric number pattern where values decrease from the edges toward the center. We’ll use two inner loops to mirror left and right halves, and a condition to decide whether to print j (outer) or i (inner).
⭐ Pattern Output
For k = 5, the pattern looks like this:
5 5 5 5 5
5 4 4 4 5
5 4 3 4 5
5 4 4 4 5
5 5 5 5 5Complete C Program
We print a left half (j = k..1) and a right half (j = 2..k) to keep the output symmetric.
#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");
}
return 0;
}🧠 How It Works
Choose the maximum value (k)
k is the outer value and controls the size of the pattern.
Outer loop controls rows
for (i = k; i >= 1; i--) moves toward the center as i decreases.
Left half (mirror start)
j = k..1 prints the left side. The condition selects the larger outer values.
Right half (mirror finish)
j = 2..k prints the right side to keep symmetry.
Decreasing center values
The center becomes smaller because when j <= i we print i instead of j.
Variation — User Input Version
Read k from the user so you can 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");
}
return 0;
}💡 Tips for Enhancement
Try These
- Let the user choose
k(shown above) - Remove spaces in
printffor a tighter look - Reverse the comparison to create an increasing-from-center variant
- Print only the border values to make a hollow version
Avoid
- Forgetting the second loop (
j = 2..k), which breaks symmetry - Using
k < 1without validating input
Key Takeaways
Two inner loops mirror the left and right halves.
The condition j > i chooses whether to print the outer value (j) or the inner value (i).
Increasing k increases the size of the pattern.
The runtime grows roughly as O(k²).
❓ Frequently Asked Questions
j keeps the outer border large; printing i creates smaller values toward the center.k (or use the input version). For example, k = 7 prints a larger pattern.k rows and roughly 2k-1 prints per row.Explore More C Number Patterns!
Master loops and conditions with more practice patterns.
This pattern prints 2k - 1 values per row. For k = 5, that’s 9 values per row across 5 rows.
12 people found this page helpful
