Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PascalTriangle
- {
- static void Main()
- {
- Console.WriteLine("Enter desired number N:");
- int height = int.Parse(Console.WriteLine());
- // const int height = 12;
- // Allocate the array in a triangle form
- long[][] triangle = new long[height + 1][];
- for (int row = 0; row < height; row++)
- {
- triangle[row] = new long[row + 1];
- }
- // Calculate the Pascal’s triangle
- triangle[0][0] = 1;
- for (int row = 0; row < n – 1; row++)
- {
- for (int col = 0; col <= row; col++)
- {
- triangle[row + 1][col] += triangle[row][col];
- triangle[row + 1][col + 1] += triangle[row][col];
- }
- }
- // Print the Pascal’s triangle
- for (int row = 0; row < height; row++)
- {
- Console.Write("".PadLeft((height – row) * 2));
- for (int col = 0; col <= row; col++)
- {
- Console.Write("{0,3} ", triangle[row][col]);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement