for i from 1 to n:
for j from 1 to 2*i - 1:
if j is even: print *
else: print i
print newline
for i from n-1 down to 1:
(same inner loop)
print newline
Cheat sheet
Goal
Pattern
Top half
for (i = 1; i <= n; i++)
Bottom half
for (i = n - 1; i >= 1; i--)
Row length 2*i-1
for (j = 1; j < i * 2; j++)
Alternate digit / star
if (j % 2 == 0) printf("*"); else printf("%d", i);
End the row
printf("\n");
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", i) / printf("*")
Stays on the same line
Digits and stars in the row
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 diamond height and the pattern updates instantly — capped at 9 so every row number stays a single character.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn 5 · 9 lines · 41 chars
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
Trace
Worked Walkthrough — n = 3
Trace the top half (i = 1..3) and bottom half (i = 2..1). Each row prints 2*i - 1 characters.
Half
i
Chars (2*i-1)
Printed row
Top
1
1
1
Top
2
3
2*2
Top
3
5
3*3*3
Bottom
2
3
2*2
Bottom
1
1
1
Total lines = 2n - 1. Total characters = n² + (n - 1)² (here 9 + 4 = 13) — why time is O(n²).
Code
C Programs
Three complete programs: fixed height, scanf input with ternary prints, and a small demo. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded height — if/else with j % 2 in both halves.
C
#include <stdio.h>
int main(void)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j < i * 2; j++)
{
if (j % 2 == 0)
printf("*");
else
printf("%d", i);
}
printf("\n");
}
for (i = 4; i >= 1; i--)
{
for (j = 1; j < i * 2; j++)
{
if (j % 2 == 0)
printf("*");
else
printf("%d", i);
}
printf("\n");
}
return 0;
}
Output
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
How It Works
1. Top half grows.i runs from 1 to 5 — rows get longer by two characters each time.
2. Inner loop alternates. Even j prints *; odd j prints the current row number i.
3. Bottom half mirrors.i runs from 4 down to 1 with the same inner logic — the peak is not repeated.
When i = 1 you get 1; when i = 3 you get 3*3*3.
Example 2 — User Input Version
Read n with scanf and clamp to 1–9. Ternary operators compact the digit-or-star choice.
C
#include <stdio.h>
int main(void)
{
int n;
int i, j;
printf("Enter n (1-9): ");
scanf("%d", &n);
if (n < 1) n = 1;
if (n > 9) n = 9;
for (i = 1; i <= n; i++)
{
for (j = 1; j < i * 2; j++)
(j % 2 == 0) ? printf("*") : printf("%d", i);
printf("\n");
}
for (i = n - 1; i >= 1; i--)
{
for (j = 1; j < i * 2; j++)
(j % 2 == 0) ? printf("*") : printf("%d", i);
printf("\n");
}
return 0;
}
Output (when user enters 3)
Enter n (1-9): 3
1
2*2
3*3*3
2*2
1
How It Works
1. Prompt and clamp. Read n, then force it into 1..9 so row numbers stay single digits.
2. Same diamond core. Only the hard-coded 5/4 become n/n - 1; ternaries replace if/else.
3. Safer input tip. Unchecked scanf leaves n uninitialized on bad input. Prefer:
Safer input
if (scanf("%d", &n) != 1 || n < 1 || n > 9)
{
printf("Enter a whole number from 1 to 9.\n");
return 1;
}
Example 3 — Compact n = 3
Same if/else logic with a smaller height for quick paper tracing.
C
#include <stdio.h>
int main(void)
{
int n = 3;
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j < i * 2; j++)
{
if (j % 2 == 0)
printf("*");
else
printf("%d", i);
}
printf("\n");
}
for (i = n - 1; i >= 1; i--)
{
for (j = 1; j < i * 2; j++)
{
if (j % 2 == 0)
printf("*");
else
printf("%d", i);
}
printf("\n");
}
return 0;
}
Output
1
2*2
3*3*3
2*2
1
How It Works
1. Same structure. Only n changes from 5 to 3 — both halves and the modulo check stay identical.
2. Trace on paper. For i = 2: j = 1, 2, 3 → digit, star, digit → 2*2.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for the classic five-high diamond.
Edge Cases & Pitfalls
Check these before calling the solution done.
one loop
Missing bottom half
A single ascending outer loop only prints a triangle. You need the second loop i = n-1..1 for the diamond.
i = n twice
Doubled peak
Starting the bottom half at n instead of n - 1 prints the longest row twice. Begin at n - 1.
% flipped
Stars first
If you print * on odd j, rows start with a star. Keep digits on odd positions (j % 2 != 0).
\n inside
Broken rows
If printf("\n") sits inside the inner loop, each character lands on its own line. Call the newline only after the row finishes.
n > 9
Multi-digit values
printf("%d", 10) prints two characters and the diamond drifts. Clamp demos to 1–9.
n = 1
Smallest diamond
Output is a single 1 — the bottom loop never runs. A good sanity check.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Small demo (Example 3)
O(n²)
O(1)
Total characters = n² + (n - 1)². For n = 5 that is 25 + 16 = 41. Extra memory stays constant aside from loop counters.
Remember
Key Takeaways
Rule: top 1..n, bottom n-1..1; even j → *, odd j → digit i.
Row length: each row prints 2*i - 1 characters via j < i * 2.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time from n² + (n-1)² chars; O(1) extra space.
One line: for each half, print 2*i-1 characters alternating digit i and *, then printf("\n").
Frequently Asked Questions
The inner loop runs while j < i*2, which prints 1, 3, 5, 7, 9 characters for i = 1..5.
It checks j % 2. Even j prints '*', odd j prints the current row number i.
The first loop builds the top half (i = 1..n). The second mirrors back down (i = n-1..1) to complete the diamond.
Program 30 is a right-aligned descending triangle. Program 31 alternates digits and stars in a symmetric diamond shape.
Replace 5 with n in both outer loops — see Example 2.
O(n²) for height n because total printed characters equal n² + (n-1)².
Check scanf's return value and reject values outside 1..9 so single-digit row numbers stay clear.