Advertisement
Venciity

[ Telerik C#] LOOPS - EX 9

Feb 13th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. // 09. In the combinatorial mathematics, the Catalan numbers are calculated by the following formula:
  2. // (2*n)! / (n + 1)! * n!
  3. // Write a program to calculate the Nth Catalan number by given N.
  4. using System;
  5.  
  6. class CatalanNumbers
  7. {
  8.     static double Factorial(double n)
  9.     {
  10.         double nFact = 1;
  11.         for (double i = 1; i <= n; i++)
  12.         {
  13.             nFact = nFact * i;
  14.         }
  15.         return nFact;
  16.     }
  17.  
  18.     static void Main()
  19.     {
  20.         double n;
  21.  
  22.         do
  23.         {
  24.             Console.Write("Enter N: ");
  25.             n = int.Parse(Console.ReadLine());
  26.         } while (n < 0);
  27.         double catalanNumber = Factorial(2 * n) / (Factorial(n + 1) * Factorial(n));
  28.         Console.WriteLine("Catalan Number = {0}", catalanNumber);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement