Filled Diamond Star Pattern in C#

What You'll Learn
You combine a center-aligned pyramid (Program 5) with its mirror: same inner loops—(rows - i) spaces, then (2 * i - 1) stars—but after i reaches rows, run i from rows - 1 down to 1 (the same outer-loop idea as Program 6).
Unlike Program 9 (hollow diamond), rows are not padded to a fixed width: the tip row is shorter; the middle row has 2 * rows - 1 stars and no leading spaces when i == rows.
⭐ Pattern Output
When you run the program with rows = 5:
*
***
*****
*******
*********
*******
*****
***
*Complete C# Program
Fixed rows = 5 version (same j / k inner loops as Program 5):
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j, k;
/* Upper half */
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
/* Lower half (no duplicate widest row) */
for (i = rows - 1; i >= 1; i--)
{
for (j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Upper half (i = 1 … rows)
for (i = 1; i <= rows; i++) builds the top pyramid: for (j = 1; j <= rows - i; j++) Console.Write(" "); then for (k = 1; k <= 2 * i - 1; k++) Console.Write("*");, then Console.WriteLine(). Star counts are 1, 3, 5, …, 2*rows-1.
Lower half (i = rows - 1 … 1)
for (i = rows - 1; i >= 1; i--) uses the same two inner loops. As i shrinks, rows - i grows (more margin) and 2 * i - 1 shrinks (fewer stars)—for rows = 5 the lower star runs are 7, 5, 3, 1.
Why 2 * i - 1?
Odd widths keep a single center star on each row. Adding one star on each side per row step preserves left–right symmetry. Each full row still prints (rows - i) + (2i - 1) = rows + i - 1 characters before the newline.
New line every row
Console.WriteLine() after the star loop ends one row of the diamond. Both outer passes rely on that same three-step pattern (spaces, stars, newline).
Solid diamond
2 * rows - 1 lines total; widest line has 2 * rows - 1 stars. O(n²) characters for n = rows, O(1) extra space. The green preview scrolls sideways on phones when the middle row is wider than the viewport.
Variation — User Input Version
Read rows with Console.ReadLine():
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j, k;
Console.Write("Enter the number of rows: ");
rows = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (i = rows - 1; i >= 1; i--)
{
for (j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Validate
rows >= 1after parsing - For a hollow diamond, use Program 9 (diagonal
j/klogic)—not a one-line tweak of the star loop - Print only the first outer loop to get Program 5’s pyramid alone
- Swap characters or print row numbers inside the star run
- Use
int.TryParsefor safer input
Avoid
- Starting the second outer loop at
i == rows—duplicate widest row - Confusing this with Program 9: hollow diamonds need fixed width
2 * rows - 1per line and different inner logic - Using
istars instead of2 * i - 1—breaks centered symmetry
Key Takeaways
Upper half = Program 5; lower half = same inners with i from rows - 1 to 1.
Stars per row: 2 * i - 1; leading spaces: rows - i.
Widest row has 2 * rows - 1 stars; tip rows are shorter (no fixed line width).
Total lines: 2 * rows - 1.
Time complexity O(n²) for n = rows.
❓ Frequently Asked Questions
i and prints wider star runs each time. The second loop shrinks i and reuses the same space and star formulas, closing the diamond.2 * rows - 1 stars is already printed when i == rows in the first loop. Continuing from rows - 1 avoids printing that line twice.*. Program 9 only places stars on two diagonals and pads each line to length 2 * rows - 1.n rows: about 2n - 1 lines, each with Θ(n) printing work in the two inner loops.Explore More C# Star Patterns!
Master Program 5 and Program 6, then this diamond is just both halves back-to-back.
The filled diamond is the same nested-loop body as Programs 5 and 6; only the sequence of i values changes: up through rows, then down from rows - 1 without repeating the peak row.
12 people found this page helpful
