Program to print a triangle of numbers I want to print the following triangle:


1
1  2
1  2  3
1  2  3  4
1  2  3  4  5
1  2  3  4
1  2  3
1  2
1



I tried to do but I cannot avoid two for loops. Is it possible to achieve the result in a single for loop?

 static void printtriangle()
{
    for (int i = 1; i &lt;= 5; i++)
    {
        for (int j = 1; j &lt;= i; j++)
        {
            Console.Write("" + (j));

        }
        Console.WriteLine("");

    }


        for (int k = 4; k &gt;= 0; --k)
        {

            for (int j = 1; j &lt;= k; j++)
            {
                Console.Write("" + (k));

            }
            Console.WriteLine("");
         }

    }
You can do it with one loop that goes from a negative number to a positive, and create a string from a range calculated from the number:

int max = 5;
for (int i = 1 - max; i &lt; max; i++) {
  Console.WriteLine(String.Join(" ", Enumerable.Range(1, max - Math.Abs(i))));
}


That is of course actually two loops, the Range method will create numbers that the Join method loops through.