CSharp · Console
using System;
public class TheExplorer
{
public static void Main()
{
// input n = diamond width
int n = int.Parse(Console.ReadLine());
/*
In the loop below the last row is n-1 because the counting starts from 0;
Bacause of the fact that the last row is n-1, then the central row will be n/2.
For example: n = 5;
1) if we start counting from 1;
so five elements will be: 12345. And the Middle element is "3"
-> center will be n/2 + 1 or 5/2 + 1 -> 2+1 = 3;
2) if we start counting from 0;
so five elements will be: 01234. As you can see mid elelemt is "2"
-> center will be n/2 or 5/2 = 2;
*/
for (int i = 0; i < n; i++)
{
// check is the row counter(i) is first or last row.
if (i == 0 || i == n - 1)
{
string outDiamond = new string('-', n / 2);
Console.WriteLine("{0}{1}{0}", outDiamond, "*");
}
// draw the central line
else if (i == (n/2))
{
string innerDiamond = new string('-', n - 2);
Console.WriteLine("{0}{1}{0}", "*", innerDiamond);
}
// Above the central line of the diamond
else if (i < (n/2))
{
string outDiamond = new string('-', n / 2 - i);
string innerDiamond = new string('-', 2*(i-1)+1);
Console.WriteLine("{0}{1}{2}{1}{0}", outDiamond, "*", innerDiamond);
}
// Every other case is covered so this is for the draw
// bellow the central line.
else
{
string outDiamond = new string('-', Math.Abs(n/2-i));
string innerDiamond = new string('-', Math.Abs(n-2 - 2*(i - n/2)));
Console.WriteLine("{0}{1}{2}{1}{0}", outDiamond, "*", innerDiamond);
}
}
}
}