A number-star diamond grows from 1 to n, then shrinks back to 1. Each row repeats the row number i times, separated by *.
Remember
Rule: 2*i-1 chars — odd j → i, even j → *
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 ← peak n = 5
In C# use two outer loops: up 1..n, then down n-1..1. The shared inner idea is j % 2 to pick a digit or a star.
Approach
How to Solve It
One row-builder idea reused for the top and bottom halves.
Method
Idea
Best for
If/else + modulus
Even j → *; odd j → i
Learning, interviews, exams
Ternary form
Same logic in one expression per cell
Shorter demos
Pseudocode
Pseudocode
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:
for j from 1 to 2*i-1:
if j is even: print * else print i
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
for (j = 1; j < i * 2; j++)
Digit or star
if (j % 2 == 0) Console.Write("*"); else Console.Write(i);
Ternary form
Console.Write((j % 2 == 0) ? "*" : i.ToString());
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit or *
Console.WriteLine
Ends the current line
After the inner loop
Try it
Live Preview
Change the peak height and the diamond updates instantly — including total row count.
Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.
Live resultpeak 5 · 9 rows
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 = 4
Trace the top climb and the mirrored bottom. Each row length is 2*i-1.
Half
i
Chars
Printed row
Top
1
1
1
Top
2
3
2*2
Top
3
5
3*3*3
Peak
4
7
4*4*4*4
Bottom
3
5
3*3*3
Bottom
2
3
2*2
Bottom
1
1
1
Total lines = 2n - 1. The peak row is printed once (only in the top loop).
Code
C# Programs
Three complete programs: fixed peak, user input with a ternary, and a compact 3-peak demo. Use View Output for sample results.
Example 1 — Fixed n = 5
Top loop climbs to 5; bottom loop mirrors from 4 down to 1. Even j prints *.
C#
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();
}
}
}
}
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.i grows from 1 to 5. Each row prints 2*i-1 characters.
2. Alternation. Odd j prints i; even j prints * — e.g. 3*3*3.
3. Bottom half.i shrinks from 4 to 1 with the same inner logic, completing the diamond.
Example 2 — User Input
Read the peak height and use a ternary for compact digit-or-star logic.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int n;
int i, j;
Console.Write("Enter n: ");
if (!int.TryParse(Console.ReadLine(), out n) || n < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
for (i = 1; i <= n; i++)
{
for (j = 1; j < i * 2; j++)
Console.Write((j % 2 == 0) ? "*" : i.ToString());
Console.WriteLine();
}
for (i = n - 1; i >= 1; i--)
{
for (j = 1; j < i * 2; j++)
Console.Write((j % 2 == 0) ? "*" : i.ToString());
Console.WriteLine();
}
}
}
}
Output (when user enters 3)
1
2*2
3*3*3
2*2
1
How It Works
1. Validate n.TryParse rejects non-numeric input; require n >= 1.
2. Same diamond. Ternaries pick digit vs star; peak 3 produces five lines total.
Example 3 — Compact n = 3
A smaller if/else demo — same two-half idea, easier to trace by hand.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int n = 3;
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j < i * 2; j++)
{
if (j % 2 == 0) Console.Write("*");
else Console.Write(i);
}
Console.WriteLine();
}
for (i = n - 1; i >= 1; i--)
{
for (j = 1; j < i * 2; j++)
{
if (j % 2 == 0) Console.Write("*");
else Console.Write(i);
}
Console.WriteLine();
}
}
}
}
Output
1
2*2
3*3*3
2*2
1
How It Works
1. Same rules. Odd positions print i; even positions print *.
2. Mirror down. After the peak, the second loop reprints rows 2 and 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
double peak
Bottom loop starts at n
That reprints the peak. Start the bottom at n - 1.
wrong length
j <= i instead of j < i * 2
You need 2*i-1 characters (digits and stars). Keep for (j = 1; j < i * 2; j++).
flip parity
Swap even/odd checks
Even j must print *. Swapping gives *2* instead of 2*2.
n = 1
Single 1
Only the top loop prints once; the bottom loop never runs.
n ≤ 0
Empty output
Neither loop runs. Validate and prompt again for clearer UX.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
If/else loops (Examples 1, 3)
O(n²)
O(1)
Ternary form (Example 2)
O(n²)
O(1)
Across both halves the printed characters sum to about 2n², so work is quadratic in n.
Remember
Key Takeaways
Rule: odd j prints i; even j prints *.
Two halves: climb 1..n, then descend n-1..1 so the peak prints once.
Write vs WriteLine: digits and stars stay on the line; WriteLine advances after each row.
Next step: Program 32 prints an increasing number triangle starting from 11.
One line: for each row value i, print 2*i-1 characters alternating i and *, then mirror the climb back down.
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 n rows because total printed characters grow quadratically across both halves.
Prefer int.TryParse(Console.ReadLine(), out n) so bad input does not throw FormatException.