A concentric number square peels layers from outer value k down to 1 at the center. Each row mirrors left and right so the grid is symmetric horizontally.
Remember
Rule: for i from k down to 1
left: j = k..1, print (j > i ? j : i)
right: j = 2..k, print (j > i ? j : i)
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 ← k = 5
Follows the X pattern in Program 45; next is the full concentric diamond in Program 47.
Approach
How to Solve It
Start with fixed k = 5, then generalize with scanf — optionally demo k = 3 for paper tracing.
Method
Idea
Best for
Layer + mirror
Outer i = k..1; left k..1 then right 2..k
Learning, interviews, exams
Ternary print
printf("%d ", j > i ? j : i)
Compact demos and labs
Pseudocode
Pseudocode
for i from k down to 1:
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
Walk each layer
for (i = k; i >= 1; i--)
Left half
for (j = k; j >= 1; j--)
Right half
for (j = 2; j <= k; j++)
Cell value
j > i ? j : i
Row width
2 * k - 1
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 concentric square 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. Layers shrink.i runs from 5 down to 1 — each pass is one concentric layer.
2. Left then right. Left prints k..1; right prints 2..k so the center digit is not doubled.
3. Cell rule. If j > i print the outer column value j; otherwise print the layer value i.
When i = 5 every cell is 5; when i = 1 the center is 1.
Example 2 — User Input k
Read k with scanf and reject non-positive values. Same logic with a ternary.
C
#include <stdio.h>
int main(void)
{
int k;
int i, j;
printf("Enter k: ");
if (scanf("%d", &k) != 1 || k <= 0)
{
printf("Please enter a positive integer.\n");
return 1;
}
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");
}
return 0;
}
Output (when user enters 3)
Enter k: 3
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
How It Works
1. Prompt and validate. Reject bad input with a clear message.
2. Same core. Width becomes 2k - 1 automatically from the two inner 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 ternary loops 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");
}
return 0;
}
Output
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
How It Works
1. Same structure. Only k changes from 5 to 3 — both halves stay identical.
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.
j = 1..k
Doubled center
If the right half starts at j = 1, the middle digit prints twice. Keep j = 2..k.
i++
Upside-down layers
Counting i up from 1 prints the center first. Use for (i = k; i >= 1; i--).
\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.
j < i
Inverted layers
Swapping the ternary to j < i breaks the concentric look. Keep j > i ? j : i.
k = 1
Smallest square
Output is a single 1 — the right half 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)
k rows × 2k - 1 cells each → about 2k² prints. For k = 5 that is 45 cells.
Remember
Key Takeaways
Rule: for each layer i, print j > i ? j : i on left k..1 then right 2..k.
Skip the center twice: the right half starts at 2 so the middle digit prints once.
Break the row: call printf("\n") only after both halves.
Complexity:O(k²) time from k × (2k - 1) cells; O(1) extra space.
One line: for i = k..1, print j > i ? j : i across left then right, then printf("\n").
Frequently Asked Questions
A concentric number square where the outer layer is k (e.g. 5) and numbers decrease toward the center, ending with 1, then mirror left and right symmetrically.
Each row prints a left half (j = k..1) and a right half (j = 2..k) with the same j > i rule, mirroring values around the center.
When column j is still outside the current row layer i, print j (the outer number). Otherwise print i (the current row value).
The first loop builds the left descending half; the second loop mirrors columns 2..k on the right without repeating the center digit.
Change k (or read it from user input). Total width becomes 2*k - 1 — see Example 2.
O(k²) because each row prints roughly 2k - 1 cells and there are k rows.
Program 45 prints a star-and-zero X on a fixed grid. Program 46 prints decreasing/increasing numbers in a concentric square.
Each row has 2*k - 1 numbers. For k = 5, width is 9 columns.
🤔
Did you know?
Each cell prints j when j > i, else i. Row i runs from k down to 1; grid width = 2k - 1 columns per row.