Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace MainProgram
- {
- class PTriangleCalc
- {
- static int GetPTInt(string prompt)
- {
- int result = 0;
- while (result < 1)
- {
- try
- {
- Console.WriteLine(prompt);
- result = int.Parse(Console.ReadLine());
- }
- catch
- {
- Console.WriteLine("Invalid Input, try again.");
- continue;
- }
- }
- return result;
- }
- static void Main()
- {
- Console.WriteLine("---Pascal's triangle row displayer---");
- int n = GetPTInt("Please enter a row that you wish to display: ");
- Console.WriteLine("- row " + n + "-");
- int[,] PTriangle = new int[n,n];
- for (int i=0; i<n; i++)
- {
- for (int j=0; j!=i; j++)
- {
- if (j == 0 || j == i)
- {
- PTriangle[i, j] = 1;
- }
- else
- {
- PTriangle[i, j] = PTriangle[i - 1, j] + PTriangle[i - 1, j - 1];
- }
- }
- }
- int[] PTRow = new int[n];
- for (int i=0; i<n; i++)
- {
- PTRow[i] = PTriangle[n - 1, i];
- }
- for (int i=0; i<n; i++)
- {
- Console.WriteLine(PTRow[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement