print Triangle of Pascal in C#

Complete program to print Triangle of Pascal


 using System;
 using System.Collections.Generic;
 
  class Pascal
  {
     static void Main()
     {
        foreach (uint[] row in GetPascalTriangle())
        {
          Print(row);
          Console.ReadKey();
       }
   }
 
    static void Print(uint[] row)
    {
       foreach (uint e in row)
          Console.Write("{0}\t", e);
       Console.WriteLine();
    }
 
    static IEnumerable<uint[]> GetPascalTriangle()
    {
       uint[] row = new uint[1] { 1 };
 
       for(int n = 2; ; n++)
       {
          yield return row;
 
          uint[] nrow = new uint[n];
         Array.Copy(row, nrow, n-1);
          for (int i = 1; i < n; i++)
             nrow[i] += row[i-1];
          row = nrow;
       }
    }
 }

The code above produces something like this:

1

1 1

1 2 1

1 3 4 1

1 4 6 4

........
By Perry    Popularity  (1572 Views)