A centered continuous number pyramid prints odd-width rows (1, 3, 5, …) with leading spaces for centering, while a single counter k keeps counting across every row.
Remember
Rule: for odd i = 1, 3, 5, …, max
pad with spaces, then print i numbers via k++
1
2 3 4
5 6 7 8 9 ← max = 5 (spaces shown)
Combines three ideas: odd widths (i += 2), a reverse column scan for pads, and a persistent k++. Follows the mirror pattern in Program 23; next is Program 25.
Approach
How to Solve It
Start with a fixed odd width, then add scanf — optionally with a re-prompt loop for bad input.
Method
Idea
Best for
Reverse scan + if (j > i)
Spaces first, then k++ numbers
Learning, interviews, exams
Safe scanf loop
Same core; re-prompt until max >= 1
Interactive programs
Pseudocode
Pseudocode
k = 1
for i from 1 to max step 2:
for j from max down to 1:
if j > i:
print space
else:
print k, then k = k + 1
print newline
Cheat sheet
Goal
Pattern
Odd row widths
for (i = 1; i <= max; i += 2)
Scan columns (pad first)
for (j = max; j >= 1; j--)
Space vs number
if (j > i) printf(" "); else printf("%d ", k++);
End the row
printf("\n");
Keep width odd
if (max % 2 == 0) max -= 1;
Start counter
k = 1; before the outer loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf(" ") / printf("%d ", k++)
Stays on the same line
Pads and numbers
printf("\n")
Ends the current line
After the inner loop
Print the whole row without a newline, then end the row once.
Try it
Live Preview
Change the max odd width and the pyramid updates instantly — even values snap down to the previous odd.
Whole numbers from 1 to 9. Even inputs become the previous odd (e.g. 6 → 5).
Live resultmax 5 · 3 rows · 9 numbers
1
2 3 4
5 6 7 8 9
Trace
Worked Walkthrough — max = 5
Trace each outer-loop value of i, how many spaces print, which numbers appear, and where k lands after the row.
i
Leading spaces
Numbers
k after
Printed row
1
4 (j = 5..2)
1
2
1
3
2 (j = 5, 4)
2 3 4
5
2 3 4
5
0
5 6 7 8 9
10
5 6 7 8 9
Leading spaces = max - i (one space per skipped column). Total numbers = 1 + 3 + 5 = 9 = ((5 + 1) / 2)².
Code
C Programs
Three complete programs: fixed max, scanf input, and a safe re-prompt loop. Use View Output to reveal sample results.
#include <stdio.h>
int main(void)
{
int i, j, k = 1;
for (i = 1; i <= 5; i += 2)
{
for (j = 5; j >= 1; j--)
{
if (j > i)
printf(" ");
else
printf("%d ", k++);
}
printf("\n");
}
return 0;
}
Output
1
2 3 4
5 6 7 8 9
How It Works
1. Start the counter.k = 1 before the outer loop so numbers continue across rows.
2. Outer loop picks odd widths.i runs 1, 3, 5 because of i += 2.
3. Reverse scan pads then prints. When j > i, print a space; otherwise print k++ with a trailing space.
4. Break the line.printf("\n") after the inner loop starts the next row.
When i = 1 you get four spaces then 1; when i = 5 you get 5 6 7 8 9 with no leading spaces.
Example 2 — User Input Version
Read max with scanf. If the user enters an even value, drop to the previous odd so row widths stay odd.
C
#include <stdio.h>
int main(void)
{
int max;
int i, j, k;
printf("Enter the maximum odd width: ");
scanf("%d", &max);
if (max % 2 == 0)
max -= 1;
if (max < 1)
return 0;
k = 1;
for (i = 1; i <= max; i += 2)
{
for (j = max; j >= 1; j--)
{
if (j > i)
printf(" ");
else
printf("%d ", k++);
}
printf("\n");
}
return 0;
}
Output (when user enters 5)
Enter the maximum odd width: 5
1
2 3 4
5 6 7 8 9
How It Works
1. Prompt and read. Ask for a max width, then store it with scanf("%d", &max).
2. Force an odd max.if (max % 2 == 0) max -= 1; — entering 6 becomes 5.
3. Same spacing + counter core. Only the source of max changes — the print logic matches Example 1.
4. Safer input tip. Unchecked scanf leaves max uninitialized on bad input — see Example 3.
Example 3 — Safe input with scanf loop
Re-prompt until scanf returns 1 and max >= 1, then draw the pyramid.
C
#include <stdio.h>
int main(void)
{
int max;
int i, j, k;
printf("Enter the maximum odd width: ");
while (scanf("%d", &max) != 1 || max < 1)
{
printf("Please enter a positive whole number: ");
/* clear leftover non-numeric characters if needed in your lab */
}
if (max % 2 == 0)
max -= 1;
k = 1;
for (i = 1; i <= max; i += 2)
{
for (j = max; j >= 1; j--)
{
if (j > i)
printf(" ");
else
printf("%d ", k++);
}
printf("\n");
}
return 0;
}
Output (when user enters 3)
Enter the maximum odd width: 3
1
2 3 4
How It Works
1. Validate first. The while loop keeps asking until scanf succeeds and max is positive.
2. Then draw as usual. Odd adjustment and the nested print loops match Examples 1–2.
3. Use this in labs. Prefer the re-prompt pattern over bare scanf so bad input does not leave max uninitialized.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset k
Numbers restart each row
If you set k = 1 inside the outer loop, every row starts over at 1. Keep k outside so values continue.
i++
Even widths
Using i++ instead of i += 2 prints every width 1..max. Keep the step at 2 for this pyramid.
\n inside
Column of values
If printf("\n") is inside the inner loop, each space or number lands on its own line. Call the newline only after the inner loop.
no spaces
Left-aligned only
Without if (j > i), you lose the centered look — every row starts at column 0.
2-digit k
Columns drift
Once k reaches 10, %d prints two characters and alignment slips. Cap demos near max 5–7 or use a fixed width like %2d.
scanf
Check the return value
If scanf fails, max may be uninitialized — use Example 3’s re-prompt loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(max²)
O(1)
Safe input (Example 3)
O(max²)
O(1)
Each of (max + 1) / 2 rows scans max columns → still quadratic in max. Number count = ((max + 1) / 2)².
Remember
Key Takeaways
Rule: odd width i; pad when j > i; else print k++.
Persistent counter: keep k outside the outer loop so numbers continue.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time from scanning n columns per row; O(1) extra space.
One line: for odd i, scan j from max down to 1 — space if j > i, else printf("%d ", k++), then printf("\n").
Frequently Asked Questions
The program prints leading spaces when j > i, which shifts the numbers to the right and centers each shorter row under the widest line.
Because the counter k is not reset inside the outer loop. It increments with k++ each time a number is printed.
Row widths are odd (1, 3, 5, …) so each row adds two more numbers than the previous row.
Spaces and numbers stay on the same line. printf("\n") ends the current row after the inner loop finishes.
The reverse inner loop lets you print leading spaces first (when j > i) and numbers afterward — a common centering trick.
Subtract 1 to force an odd width (see Example 2), or validate and prompt again.
O(n²) for max width n because each row iterates across n columns.
Check scanf's return value and re-prompt until you get a positive integer (see Example 3).
🤔
Did you know?
This centered pyramid prints numbers continuously using a counter k. An if inside a reverse loop prints leading spaces when j > i, then prints k++ once the column reaches the row boundary — k never resets between rows.