Show a diamond shape with numbers After seeing this post on StackOverflow, I thought I'd try it out. What I have now is a diamond shape with numbers like this:

    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1


My code looks like this:

int i, j, k, n = 6, l = 3, o = 2;
for (i = 1; i &lt;= 5; i++)
{
    for (k = n; k &gt;= 1; k--)
    {
        Console.Write(" ");
    }
    n--;
    for (j = i; j &gt;= 1; j--)
    {
        Console.Write(j);
    }
    o = 2;
    for (j = 1; j &lt; i; j++)
    {
        Console.Write(o);
        o++;
    }
    Console.WriteLine();
}
for (i = 4; i &gt;= 1; i--)
{
    for (k = l; k &gt;= 1; k--)
    {
        Console.Write(" ");
    }
    l++;
    for (j = i; j &gt;= 1; j--)
    {
        Console.Write(j);
    }
    o = 2;
    for (j = i; j &gt; 1; j--)
    {
        Console.Write(o);
        o++;
    }
    Console.WriteLine();
}
Console.Read();


I don't think this is a nice solution for this task. Is there any nicer and shorter way to solve this without using that many loops?
Code issues that decrease readability and maintainability


Plenty of loops.  
Non-meaningful variable names: n, l, o.




We can declare the following helper methods:

private static string CreateDigitsString(int length)
{
    char[] buffer = new char[length];
    for (int i = 0; i &lt; buffer.Length; i++)
    {
        buffer[i] = (char)('1' + Math.Abs(buffer.Length / 2 - i));
    }
    return new string(buffer);
}

private static string CreateLine(int length, int spaces)
{
    return new string(' ', spaces) + CreateDigitsString(length - 2 * spaces) + new string(' ', spaces);
}


Then use it like:

const int N = 5;
const int TotalRows = N * 2 - 1;

for (int row = 0; row &lt; TotalRows; row++)
{
    Console.WriteLine(CreateLine(TotalRows, Math.Abs(N - row - 1)));
}




Another approach
In the 2-dimensional loop (i, j) we can calculate the distance between the current cell and the center of the diamond as follows:

int rowDistance = Math.Abs(row - N + 1);
int colDistance = Math.Abs(col - N + 1);


Let's output the colDistance for cells that are within rowDistance + colDistance &lt;= N - 1, and space - otherwise.

const int N = 5;
const int TotalSize = N * 2 - 1;

for (int row = 0; row &lt; TotalSize; row++)
{
    for (int col = 0; col &lt; TotalSize; col++)
    {
        int rowDistance = Math.Abs(row - N + 1);
        int colDistance = Math.Abs(col - N + 1);
        Console.Write(rowDistance + colDistance &lt;= N - 1 ? (char)('1' + colDistance) : ' ');
    }
    Console.WriteLine();
}