Advertisement
Guest User

02162012byKrissam

a guest
Feb 16th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace PascalTriangle
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             ShowPascal(15);
  10.             Console.Read();
  11.  
  12.         }
  13.         private static int PascalsCoords(int x, int y)
  14.         {
  15.             if (y > x)
  16.             {
  17.                 Console.WriteLine("Error, unknown");
  18.                 return -1;
  19.             }
  20.             return Pascals(x - (y - 1), y);
  21.         }
  22.  
  23.         private static int Pascals(int x, int y)
  24.         {
  25.             if (x == 1 || y == 1) { return 1; }
  26.  
  27.             return Pascals(x, y - 1) + Pascals(x - 1, y); ;
  28.         }
  29.         private static void ShowPascal(int height)
  30.         {
  31.             int width = 80;
  32.             int fieldWidth = 5;
  33.             for (int i = 1; i <= height; i++)
  34.             {
  35.                 string leading = new String(' ', (width / 2) - (fieldWidth*i/2));
  36.                 Console.Write(leading);
  37.  
  38.                 for (int j = 1; j <= i; j++)
  39.                 {
  40.                     string output = "" + PascalsCoords(i, j);
  41.                     output = output.PadRight(fieldWidth);
  42.                     Console.Write(output);
  43.                 }
  44.                 Console.WriteLine();
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement