Advertisement
Masovski

[C# Basics][Loops-HW] 8. Catalan Numbers

Mar 29th, 2014
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class CatalanNumbers
  5. {
  6.     static void Main()
  7.     {
  8.         Console.Write("Enter n: ");
  9.         double n = double.Parse(Console.ReadLine());
  10.         bool inRange = n > 1 && n < 100;
  11.  
  12.         if (inRange)
  13.         {
  14.             double nPlusOne = n + 1;
  15.  
  16.             BigInteger nFactorial = 1;
  17.             BigInteger nPlusOneFactorial = 1;
  18.             BigInteger doubleNFactorial = 1;
  19.             BigInteger result = 0;
  20.  
  21.             for (int i = 1; i <= 2 * n; i++)
  22.             {
  23.                 doubleNFactorial *= i;
  24.  
  25.                 if (i <= n)
  26.                 {
  27.                     nFactorial *= i;
  28.                 }
  29.                 if (i <= nPlusOne)
  30.                 {
  31.                     nPlusOneFactorial *= i;
  32.                 }
  33.             }
  34.             result = doubleNFactorial / (nPlusOneFactorial * nFactorial);
  35.             Console.WriteLine("{0}", result);
  36.         }
  37.         else
  38.         {
  39.             Console.WriteLine("Invalid input. Correct input --> 1 < n < 100");
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement