Start with fixed levels = 5, then generalize with scanf — optionally demo a smaller count for paper tracing.
Method
Idea
Best for
Top then bottom
Grow i = 1..levels, then mirror levels-1..1
Learning, interviews, exams
User input levels
Same loops; read levels with scanf
Reusable demos and labs
Pseudocode
Pseudocode
for i from 1 to levels:
print (levels - i) spaces
print k from 1 to (2*i - 1)
print newline
for i from levels-1 down to 1:
print (levels - i) spaces
print k from 1 to (2*i - 1)
print newline
Cheat sheet
Goal
Pattern
Top half
for (i = 1; i <= levels; i++)
Bottom half
for (i = levels - 1; i >= 1; i--)
Top spaces
for (j = i; j < levels; j++) printf(" ");
Bottom spaces
for (j = levels; j > i; j--) printf(" ");
Digits on row i
for (k = 1; k < i * 2; k++) printf("%d", k);
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf(" ") / printf("%d", k)
Stays on the same line
Each space and each digit
printf("\n")
Ends the current line
After spaces + digits
Print spaces and digits without a newline, then end the row once.
Try it
Live Preview
Change the level count and the centered diamond updates instantly — capped at 5 so every digit stays a single character.
Whole numbers from 1 to 5. Tap a chip or type a value — the preview redraws as you go.
Live result5 levels · 9 rows · 41 digits
1
123
12345
1234567
123456789
1234567
12345
123
1
Trace
Worked Walkthrough — levels = 3
Trace every row of a small diamond — spaces, digit count, and printed output.
Half
i
Spaces
Digits
Printed row
Top
1
2
1
1
Top
2
1
1, 2, 3
123
Top
3
0
1..5
12345
Bottom
2
1
1, 2, 3
123
Bottom
1
2
1
1
Total lines = 2*levels - 1 (here 5). Digits grow as odd numbers and mirror — overall work is O(n²).
Code
C Programs
Three complete programs: fixed levels, scanf input, and a compact demo. Use View Output to reveal sample results.
Example 1 — Fixed levels = 5
Hard-coded diamond — top half grows to the peak, bottom half mirrors back down.
C
#include <stdio.h>
int main(void)
{
int i, j, k;
for (i = 1; i <= 5; i++)
{
for (j = i; j < 5; j++)
printf(" ");
for (k = 1; k < i * 2; k++)
printf("%d", k);
printf("\n");
}
for (i = 4; i >= 1; i--)
{
for (j = 5; j > i; j--)
printf(" ");
for (k = 1; k < i * 2; k++)
printf("%d", k);
printf("\n");
}
return 0;
}
Output
1
123
12345
1234567
123456789
1234567
12345
123
1
How It Works
1. Top half grows.i runs from 1 to 5 — each row gets one fewer space and two more digits.
2. Digits are odd-length.k runs while k < i * 2, so row i prints 2*i - 1 digits.
3. Bottom half mirrors.i runs from 4 down to 1 with the same digit rule and a matching space count.
When i = 1 you get four spaces and 1; when i = 3 you get 12345.
Example 2 — User Input Levels
Read levels with scanf and reject non-positive values. Same top/bottom core.
C
#include <stdio.h>
int main(void)
{
int levels;
int i, j, k;
printf("Enter levels: ");
if (scanf("%d", &levels) != 1 || levels <= 0)
{
printf("Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= levels; i++)
{
for (j = i; j < levels; j++)
printf(" ");
for (k = 1; k < i * 2; k++)
printf("%d", k);
printf("\n");
}
for (i = levels - 1; i >= 1; i--)
{
for (j = levels; j > i; j--)
printf(" ");
for (k = 1; k < i * 2; k++)
printf("%d", k);
printf("\n");
}
return 0;
}
Output (when user enters 3)
Enter levels: 3
1
123
12345
123
1
How It Works
1. Prompt and validate. Reject bad input with a clear message instead of looping on garbage.
2. Same core. Only the hard-coded 5 / 4 become levels and levels - 1.
3. Safer input tip. Cap demos so peak digits stay single characters:
Safer input
if (scanf("%d", &levels) != 1 || levels < 1 || levels > 5)
{
printf("Enter a whole number from 1 to 5.\n");
return 1;
}
Example 3 — Compact levels = 3
Same space and digit loops with a smaller level count for quick paper tracing.
C
#include <stdio.h>
int main(void)
{
int levels = 3;
int i, j, k;
for (i = 1; i <= levels; i++)
{
for (j = i; j < levels; j++)
printf(" ");
for (k = 1; k < i * 2; k++)
printf("%d", k);
printf("\n");
}
for (i = levels - 1; i >= 1; i--)
{
for (j = levels; j > i; j--)
printf(" ");
for (k = 1; k < i * 2; k++)
printf("%d", k);
printf("\n");
}
return 0;
}
Output
1
123
12345
123
1
How It Works
1. Same structure. Only levels changes from 5 to 3 — both halves stay identical.
2. Trace on paper. For i = 2: one space, then 123 — the middle of the small diamond.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five levels or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
one half
Triangle instead of diamond
Skipping the bottom loop leaves only a pyramid. Keep both halves: 1..levels then levels-1..1.
i = levels..1
Duplicated peak
If the bottom starts at levels, the widest row prints twice. Start at levels - 1.
\n inside
Broken rows
If printf("\n") sits inside a digit loop, each digit lands on its own line. Call the newline only after spaces and digits.
levels > 5
Multi-digit values
When 2*i-1 >= 10, digits become two characters and the diamond drifts. Clamp demos to 1–5.
levels = 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, levels may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(levels²)
O(1)
Small demo (Example 3)
O(levels²)
O(1)
About 2*levels - 1 rows, each printing O(levels) characters → O(n²). For levels = 5 that is 9 rows and 41 digits.
Remember
Key Takeaways
Rule: top 1..levels, bottom levels-1..1; each row prints levels - i spaces then 1..(2*i-1).
Odd widths: row length is always 2*i - 1 — that is what forms the diamond silhouette.
Break the row: call printf("\n") only after spaces and digits.
Complexity:O(n²) time from mirrored odd rows; O(1) extra space.
One line: for each half, print levels - i spaces then digits 1..(2*i-1), then printf("\n").
Frequently Asked Questions
A centered diamond of ascending digit sequences: 1, 123, 12345, 1234567, 123456789, then mirrors back down to 1.
The row prints 2*i-1 digits (odd length): 1, 3, 5, 7, 9, ... using for (k = 1; k < i*2; k++).
A space loop prints leading spaces before the numbers. Top half uses for (j = i; j < levels; j++); bottom half uses for (j = levels; j > i; j--).
The first loop builds the top half (i = 1..levels). The second mirrors back down (i = levels-1..1) to complete the diamond.
Program 43 is a right-aligned triangle with fixed-width columns. Program 44 is a symmetric centered diamond with odd-length digit rows.
Replace 5 with a levels variable in both outer loops and space bounds — see Example 2.
O(n²) for n levels because each level prints O(n) digits and there are O(n) levels (top and bottom).
Check scanf's return value and reject non-positive values so both outer loops have a valid range.
🤔
Did you know?
This pattern prints a top half (1..levels) and a bottom half (levels-1..1). Each row prints 2*i-1 digits (1 to 2*i-1) with leading spaces to center the diamond.