Advertisement
Guest User

Pasgays Triangle

a guest
Dec 18th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MainProgram
  4. {
  5.     class PTriangleCalc
  6.     {
  7.         static int GetPTInt(string prompt)
  8.         {
  9.             int result = 0;
  10.  
  11.             while (result < 1)
  12.             {
  13.                 try
  14.                 {
  15.                     Console.WriteLine(prompt);
  16.                     result = int.Parse(Console.ReadLine());
  17.                 }
  18.                 catch
  19.                 {
  20.                     Console.WriteLine("Invalid Input, try again.");
  21.                     continue;
  22.                 }
  23.             }
  24.             return result;
  25.         }
  26.  
  27.         static void Main()
  28.         {
  29.             Console.WriteLine("---Pascal's triangle row displayer---");
  30.             int n = GetPTInt("Please enter a row that you wish to display: ");
  31.             Console.WriteLine("- row " + n + "-");
  32.  
  33.             int[,] PTriangle = new int[n,n];
  34.  
  35.             for (int i=0; i<n; i++)
  36.             {
  37.                 for (int j=0; j!=i; j++)
  38.                 {
  39.                     if (j == 0 || j == i)
  40.                     {
  41.                         PTriangle[i, j] = 1;
  42.                     }
  43.                     else
  44.                     {
  45.                         PTriangle[i, j] = PTriangle[i - 1, j] + PTriangle[i - 1, j - 1];
  46.                     }
  47.                 }
  48.             }
  49.  
  50.  
  51.             int[] PTRow = new int[n];
  52.  
  53.             for (int i=0; i<n; i++)
  54.             {
  55.                 PTRow[i] = PTriangle[n - 1, i];
  56.             }
  57.  
  58.             for (int i=0; i<n; i++)
  59.             {
  60.                 Console.WriteLine(PTRow[i]);
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement