C# Display a Pascal Triangle

The rows of Pascal’s triangle are conventionally enumerated starting with row n = 0 at the top (the 0th row). The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the numbers in the adjacent rows. The triangle may be constructed in the following manner: In row 0 (the topmost row), there is a unique nonzero entry 1. Each entry of each subsequent row is constructed by adding the number above and to the left with the number above and to the right, treating blank entries as 0. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row. (wiki)

 

using System;

class Program
{
    public static void Main()
    {
        int[,] arr = new int[8, 8];

        Console.WriteLine("Pascal Triangle: ");

        for (int i = 0; i < 5; i++) { for (int k = 5; k > i; k--)
            {
                Console.Write(" ");
            }

            for (int j = 0; j < i; j++)
            {
                if (j == 0 || i == j)
                {
                    arr[i, j] = 1;
                }
                else
                {
                    arr[i, j] = arr[i - 1, j] + arr[i - 1, j - 1];
                }
                Console.Write(arr[i, j] + " ");
            }

            Console.Write("\n");
            
        }

        Console.ReadLine();
    }
}

 

Output