Advertisement
v_staykov

Catalan Number

Dec 5th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class CatalanNumbers
  5. {
  6.     static void Main()
  7.     {
  8.         Console.WriteLine("This program prints the Nth Catalan number.");
  9.         Console.WriteLine("The index of the Catlan numbers starts from 0.\n");
  10.  
  11.         Console.Write("Please insert value for N: ");
  12.         string consoleInput;
  13.         consoleInput = Console.ReadLine();
  14.  
  15.  
  16.         uint catalanNumber;
  17.  
  18.         if (uint.TryParse(consoleInput, out catalanNumber) && catalanNumber >= 0)
  19.         {
  20.             BigInteger resultNumber = 1;
  21.             BigInteger divisor = 1;
  22.  
  23.             for (uint i = 1; i <= 2 * catalanNumber; i++)
  24.             {
  25.                 if (i <= catalanNumber + 1)
  26.                 {
  27.                     divisor *= i;
  28.                 }
  29.                 if (i >= catalanNumber + 1)
  30.                 {
  31.                     resultNumber *= i;
  32.                 }
  33.             }
  34.             resultNumber /= divisor;
  35.             Console.WriteLine("The {0} Catalan number is {1}.", catalanNumber, resultNumber);
  36.         }
  37.         else
  38.         {
  39.             Console.WriteLine("Wrong input, N must be >=0!");
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement