Number-Star Diamond Pattern in C#

What You’ll Learn
How to print a diamond-like pattern that alternates between the row number and * characters:
1, 2*2, 3*3*3, ... then back down to 1.
⭐ Pattern Output
For n = 5, the pattern looks like this:
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
11
Complete C# Program
This is the exact reference approach: print the top half with i = 1..5, then print the bottom half with i = 4..1. Use j % 2 to alternate.
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();
}
}
}
}2
Variation — User Input n
Make the height configurable:
C#
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();
}
}
}
}12 people found this page helpful
