Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- class CatalanNumbers
- {
- static void Main()
- {
- Console.WriteLine("This program prints the Nth Catalan number.");
- Console.WriteLine("The index of the Catlan numbers starts from 0.\n");
- Console.Write("Please insert value for N: ");
- string consoleInput;
- consoleInput = Console.ReadLine();
- uint catalanNumber;
- if (uint.TryParse(consoleInput, out catalanNumber) && catalanNumber >= 0)
- {
- BigInteger resultNumber = 1;
- BigInteger divisor = 1;
- for (uint i = 1; i <= 2 * catalanNumber; i++)
- {
- if (i <= catalanNumber + 1)
- {
- divisor *= i;
- }
- if (i >= catalanNumber + 1)
- {
- resultNumber *= i;
- }
- }
- resultNumber /= divisor;
- Console.WriteLine("The {0} Catalan number is {1}.", catalanNumber, resultNumber);
- }
- else
- {
- Console.WriteLine("Wrong input, N must be >=0!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement