aspire12

2.PascalTriangle

Mar 29th, 2020
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _2.PascalTriangle
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int rows = int.Parse(Console.ReadLine());
  10.             int[][] triangleArray = new int[rows][];
  11.             for (int i = 0; i < rows; i++)
  12.             {
  13.                 triangleArray[i] = new int[i + 1];
  14.             }
  15.             triangleArray[0][0] = 1;
  16.             Console.WriteLine(triangleArray[0][0]);
  17.             for (int i = 1; i < triangleArray.GetLength(0); i++)
  18.             {
  19.                 for (int j = 0; j < triangleArray[i].Length; j++)
  20.                 {
  21.                     if(j == 0 || j == triangleArray[i].Length - 1)
  22.                     {
  23.                         triangleArray[i][j] = 1;
  24.                         Console.Write(triangleArray[i][j] + " ");
  25.                     }
  26.                     else
  27.                     {
  28.                         triangleArray[i][j] = triangleArray[i - 1][j - 1] + triangleArray[i-1][j];
  29.                         Console.Write(triangleArray[i][j] + " ");
  30.                     }
  31.                 }
  32.                 Console.WriteLine();
  33.             }
  34.  
  35.            //foreach(int[] array in triangleArray)
  36.            //{
  37.            //    foreach (int position in array)
  38.            //    {
  39.            //        Console.Write(position + " ");
  40.            //    }
  41.            //    Console.WriteLine();
  42.            //}
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment