From 2a43a311d8a91b71865f2e666d0fba7e8d29274c Mon Sep 17 00:00:00 2001 From: deepak7336 <70963318+deepak7336@users.noreply.github.com> Date: Sat, 1 Oct 2022 00:59:16 +0530 Subject: [PATCH] Create nth Catalan Number Catalan numbers are defined as a mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations. The nth term in the sequence denoted Cn, is found in the following formula: --- C++/nth Catalan Number | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 C++/nth Catalan Number diff --git a/C++/nth Catalan Number b/C++/nth Catalan Number new file mode 100644 index 0000000..1dee4c9 --- /dev/null +++ b/C++/nth Catalan Number @@ -0,0 +1,26 @@ +#include +using namespace std; + +// A recursive function to find nth catalan number +unsigned long int catalan(unsigned int n) +{ + // Base case + if (n <= 1) + return 1; + + // catalan(n) is sum of + // catalan(i)*catalan(n-i-1) + unsigned long int res = 0; + for (int i = 0; i < n; i++) + res += catalan(i) * catalan(n - i - 1); + + return res; +} + +// Driver code +int main() +{ + for (int i = 0; i < 10; i++) + cout << catalan(i) << " "; + return 0; +}