Shape Rule
Diamond halves
Top half grows 1..n; bottom half mirrors n-1..1.

The number-star diamond prints 1, 2*2, 3*3*3, … 5*5*5*5*5, then mirrors back down — a natural step after the right-aligned triangle in Program 30. This tutorial covers two outer loops, modulus alternation, a live preview, worked C# examples, edge cases, and complexity.
Diamond halves
Top half grows 1..n; bottom half mirrors n-1..1.
i = 1..n
for (i = 1; i <= n; i++) — builds the growing half of the diamond.
i = n-1..1
for (i = n - 1; i >= 1; i--) — mirrors the top half back down.
Alternate fill
Odd j prints i; even j prints *.
Height 3–7
Pick a height and draw the number-star diamond in the browser.
Complexity
Each row prints 2*i-1 chars — total work scales as n².
A number-star diamond pattern alternates the row number and * on each line, growing to a peak then mirroring back down. With n = 5, you get 1, 2*2, … 5*5*5*5*5, then the same rows in reverse.
In C# you use two outer loops (top and bottom halves) and j % 2 inside the inner loop to alternate digit and star.
It combines symmetric diamond logic with the modulus operator — a step up from Program 30’s single-loop triangle.
Inner loop runs j < i*2.
Odd prints i, even prints *.
Top 1..n, bottom n-1..1.
Follow Program 30; continue to Program 32 (triangle from 11) next.
In short: top loop i = 1..n, bottom loop i = n-1..1, inner j % 2 alternates digit and star, then WriteLine().
Given n = 5, print a number-star diamond: top half i = 1..n, bottom half i = n-1..1, each row alternating digit i and * via j % 2.
// n = 5 (conceptual shape)
// 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 | Item | Type | Description |
|---|---|---|
n | int | Diamond peak height — total lines = 2*n - 1. |
i | int | Outer loop — current row number printed on odd positions. |
j | int | Inner loop — j % 2 == 0 prints *, else prints i. |
for i from 1 to n:
for j from 1 to i*2 - 1:
if j % 2 == 0: print *
else: print i
print newline
for i from n-1 down to 1:
for j from 1 to i*2 - 1:
if j % 2 == 0: print *
else: print i
print newline | Approach | Idea | Best for |
|---|---|---|
| if/else | 1, 2*2, 3*3*3, … | Learning and interviews |
| Ternary operator | (j % 2 == 0) ? "*" : i.ToString() | Compact console programs |
| User-input n | int n = Convert.ToInt32(...) | Flexible diamond height |
| Goal | Pattern |
|---|---|
| Top half | for (i = 1; i <= n; i++) |
| Bottom half | for (i = n - 1; i >= 1; i--) |
| Inner loop | for (j = 1; j < i * 2; j++) |
| Alternate fill | if (j % 2 == 0) Console.Write("*"); else Console.Write(i); |
| Ternary form | Console.Write((j % 2 == 0) ? "*" : i.ToString()); |
| User input | int n = Convert.ToInt32(Console.ReadLine()); |
Same number-star diamond — different ways to write the modulus check and control height.
i = 1..nGrowing rows to the peak
i = n-1..1Mirror back down
j%2==0 ? * : iAlternate star and digit
2*i-1Characters per row
Reach for this pattern when teaching symmetric diamonds, the modulus operator, and two-phase loop structures.
Natural follow-up after Program 30 — introduces modulus and a mirrored bottom half.
Outer/inner bound practice with an immediate visual check.
Combine loops with ReadLine for a flexible row count.
Compare Program 30 (right-aligned triangle) and Program 32 (triangle from 11) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a height between 3 and 7 and draw the number-star diamond in the browser.
Three complete C# programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print a full diamond with n = 5 using if/else and j % 2.
n = 5Hard-coded row count — ideal for first demos and screenshots.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j < i * 2; j++)
{
if (j % 2 == 0)
Console.Write("*");
else
Console.Write(i);
}
Console.WriteLine();
}
for (i = 4; i >= 1; i--)
{
for (j = 1; j < i * 2; j++)
{
if (j % 2 == 0)
Console.Write("*");
else
Console.Write(i);
}
Console.WriteLine();
}
}
}
} When i = 1, the inner loop prints one character — 1. When i = 3, it prints 3*3*3 (five characters). The bottom half mirrors from i = 4 down to 1.
Read the row count from the console instead of hard-coding 5.
Read n from the console to control diamond height.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter n: ");
int n = Convert.ToInt32(Console.ReadLine());
if (n < 1) return;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < i * 2; j++)
Console.Write((j % 2 == 0) ? "*" : i.ToString());
Console.WriteLine();
}
for (int i = n - 1; i >= 1; i--)
{
for (int j = 1; j < i * 2; j++)
Console.Write((j % 2 == 0) ? "*" : i.ToString());
Console.WriteLine();
}
}
}
} Same diamond core as Example 1; a ternary operator replaces if/else and n replaces hard-coded 5.
Run with n = 3 to trace every row on paper before scaling up.
n = 3Same if/else logic with a smaller row count for quick tracing.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int n = 3;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < i * 2; j++)
{
if (j % 2 == 0) Console.Write("*");
else Console.Write(i);
}
Console.WriteLine();
}
for (int i = n - 1; i >= 1; i--)
{
for (int j = 1; j < i * 2; j++)
{
if (j % 2 == 0) Console.Write("*");
else Console.Write(i);
}
Console.WriteLine();
}
}
}
} Only n changes from 5 to 3 — the if/else and two-loop structure stay identical. Trace i = 1, 2, 3 on paper to see how row length grows as 2*i-1.
using System; brings in Console. Set loop variables i, j with n = 5.
for (i = 1; i <= n; i++) — growing rows from 1 to the peak.
for (j = 1; j < i * 2; j++) — prints 2*i-1 characters per row.
j % 2 == 0 prints *; odd j prints i.
for (i = n - 1; i >= 1; i--) — mirrors the top half back down.
2*n-1 total rows — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, inner-loop range, character count, and full row output.
i | Inner range (j) | Chars | Row output |
|---|---|---|---|
1 | 1 | 1 | 1 |
2 | 1, 2, 3 | 3 | 2*2 |
3 | 1..5 | 5 | 3*3*3 |
4 | 1..7 | 7 | 4*4*4*4 |
5 | 1..9 | 9 | 5*5*5*5*5 |
Characters per row = 2*i-1. Bottom half repeats rows 4, 3, 2, 1 in reverse.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: flip j % 2 logic and watch stars land on wrong positions.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 32 for a triangle starting from 11.
Practice Write vs WriteLine without complex math.
Example: put WriteLine inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use Console.Write(j + " ") between digits for wider spacing.
Triangular totals make O(n²) concrete for beginners.
Example: count printed characters for n = 5 — top half alone prints 25 chars.
Pair the pattern with TryParse and positive-row checks.
Example: reject max <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C# courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i and j on paper for n = 3 before coding — watch how row length grows as 2*i-1.
Small habits that keep number-pattern code clean.
Top half 1..n and bottom half n-1..1 — do not repeat the peak row.
TryParseAvoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
Mark odd/even positions for each row before coding the alternation.
Trace i = 1..3 on paper before coding the full n = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put WriteLine inside the inner loop.
Mistakes that commonly break number-star diamond patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use Write(i) or Write("*"); WriteLine only after the inner loop.
Using j % 2 != 0 for stars (instead of == 0) swaps digit and star positions.
→ Even j prints *; odd j prints i.
j <= i * 2 adds an extra character — row length becomes even instead of odd.
→ Keep for (j = 1; j < i * 2; j++) for exactly 2*i-1 chars.
Starting the bottom loop at i = n prints the widest row twice.
→ Bottom half starts at i = n - 1, not n.
Letters or empty input throw FormatException.
→ Prefer int.TryParse and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — one row, no bottom half needed.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Three rows: 1, 2*2, 1.
Convert.ToInt32 throws — use TryParse.
Total lines = 2*n - 1 — grows quadratically with peak height.
Try these variations to lock in the pattern.
j > ii = 1..n without the mirror* with # or .j % 2 logic, different symbolj prints i; even j prints *. Inner loop runs j < i*2.Console.Write stays on the line; WriteLine advances — mix them carefully.n > 0 for interactive programs; n = 1 prints a single 1.n - 1 — do not repeat the peak row at i = n.Quick Takeaway: top loop i = 1..n, bottom i = n-1..1, inner j % 2 alternates digit and star, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The number-star diamond is a compact lesson in symmetric patterns and the modulus operator: alternate i and * with j % 2, grow rows in the top half, then mirror back down. Master the fixed-n version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 32 for the increasing number triangle starting from 11.
Bottom half must start at n - 1 — validate n when reading from the console.
for (i = 1; i <= n; i++)for (i = n - 1; i >= 1; i--)j % 2 == 0 prints *, else prints iint.TryParse over bare Convert.ToInt32WriteLine inside the inner loopi = n (repeats peak row)j <= i * 2 instead of j < i * 2n = 1 edge casePrint the pattern the beginner-friendly way.
j%2: * or i
DefinitionTop + mirror
Code2*i-1 chars
Codei = n-1
ShapeO(n²) time
AnalysisThis pattern prints a top half (1..n) and a bottom half (n-1..1). Each row prints 2*i-1 characters, alternating the row number and * using j % 2.
Move on to the increasing number triangle starting from 11 in the C# number-pattern series.
12 people found this page helpful