A concentric number diamond peels layers from outer value k down to 1 at the center, then mirrors back out to k. Program 46 was the top half only — this page adds the bottom half.
Follows the concentric square in Program 46; next is the powers-of-11 sequence in Program 48.
Approach
How to Solve It
Reuse Program 46’s cell logic, then add a second outer loop for the bottom half — skip i = 1 so the center row is not duplicated.
Method
Idea
Best for
Two outer loops
Top i = k..1, bottom i = 2..k; same left/right halves
Learning, interviews, exams
Ternary print
printf("%d ", j > i ? j : i)
Compact demos and labs
Pseudocode
Pseudocode
for i from k down to 1: // top half
for j from k down to 1:
print (j if j > i else i) and a space
for j from 2 to k:
print (j if j > i else i) and a space
print newline
for i from 2 to k: // bottom half
for j from k down to 1:
print (j if j > i else i) and a space
for j from 2 to k:
print (j if j > i else i) and a space
print newline
Cheat sheet
Goal
Pattern
Top half
for (i = k; i >= 1; i--)
Bottom half
for (i = 2; i <= k; i++)
Left half
for (j = k; j >= 1; j--)
Right half
for (j = 2; j <= k; j++)
Cell value
j > i ? j : i
Grid size
2 * k - 1 rows and columns
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d ", value)
Stays on the same line
Each cell in both halves
printf("\n")
Ends the current line
After both inner loops
Print all cells without a newline, then end the row once.
Try it
Live Preview
Change k and the full diamond updates instantly — capped at 9 so every value stays a single digit.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
1. Prompt and validate. Reject bad input with a clear message.
2. Same core. Grid becomes (2k - 1) × (2k - 1) automatically from the two outer loops.
3. Safer input tip. Cap demos to single-digit layers:
Safer input
if (scanf("%d", &k) != 1 || k < 1 || k > 9)
{
printf("Enter a whole number from 1 to 9.\n");
return 1;
}
Example 3 — Compact k = 3
Same two-pass structure with a smaller outer value for quick paper tracing.
C
#include <stdio.h>
int main(void)
{
int k = 3;
int i, j;
for (i = k; i >= 1; i--)
{
for (j = k; j >= 1; j--)
printf("%d ", (j > i ? j : i));
for (j = 2; j <= k; j++)
printf("%d ", (j > i ? j : i));
printf("\n");
}
for (i = 2; i <= k; i++)
{
for (j = k; j >= 1; j--)
printf("%d ", (j > i ? j : i));
for (j = 2; j <= k; j++)
printf("%d ", (j > i ? j : i));
printf("\n");
}
return 0;
}
Output
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
How It Works
1. Five rows total. Three from the top loop, two from the bottom (center already printed).
2. Trace on paper. For i = 2: left prints 3 2 2, right prints 2 3 → 3 2 2 2 3.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for k = 5 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
i = 1 twice
Doubled center row
If the bottom loop starts at i = 1, the middle row prints twice. Keep for (i = 2; i <= k; i++).
missing bottom
Square instead of diamond
Omitting the second outer loop leaves only Program 46’s top half. Add i = 2..k after the center.
j = 1..k
Doubled center digit
If the right half starts at j = 1, the middle digit prints twice. Keep j = 2..k.
\n inside
Broken rows
If printf("\n") sits inside either half, each cell lands on its own line. Call the newline only after both halves.
k = 1
Smallest diamond
Output is a single 1 — the bottom loop does not run. A good sanity check.
scanf
Check the return value
If scanf fails, k may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(k²)
O(1)
Compact k = 3 (Example 3)
O(k²)
O(1)
(2k - 1) rows × (2k - 1) cells → about 4k² prints. For k = 5 that is 81 cells.
Remember
Key Takeaways
Two passes: top i = k..1, bottom i = 2..k — same left/right halves and j > i ? j : i rule.
Skip the center twice: bottom starts at 2; right half starts at 2 so neither the center row nor the middle digit repeats.
Break the row: call printf("\n") only after both halves.
Complexity:O(k²) time from (2k - 1)² cells; O(1) extra space.
One line: top i = k..1, bottom i = 2..k, cell rule j > i ? j : i, then printf("\n").
Frequently Asked Questions
A full concentric number diamond where the outer layer is k (e.g. 5), values decrease to 1 at the center, then increase back to k — a 9×9 grid when k = 5.
The first prints the top half (i = k down to 1). The second prints the bottom half (i = 2 up to k) to mirror the shape without repeating the center row.
Program 46 prints only the top half (k rows). Program 47 adds the bottom half loop to form a complete diamond.
When column j is still outside the current row layer i, print j (the outer number). Otherwise print i (the current row value).
For k, each row has 2*k - 1 numbers. With k = 5, width is 9 columns and 9 rows.
Change the value of k or read it from user input — both outer loops and row width adjust automatically.
O(k²) because the grid has roughly (2k-1)² cells and each is printed once.
The center prints 1 — it is the deepest layer of the concentric pattern.
🤔
Did you know?
Print top half with for (i = k; i >= 1; i--), then bottom half with for (i = 2; i <= k; i++). Each cell: j > i ? j : i. Grid size = 2k - 1 rows and columns.