diff --git a/Maths/CatalanNumber.cpp b/Maths/CatalanNumber.cpp new file mode 100644 index 00000000..7108db34 --- /dev/null +++ b/Maths/CatalanNumber.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +unsigned long long catalanDP(int n) { + vector catalan(n + 1, 0); + + catalan[0] = 1; + catalan[1] = 1; + + for (int i = 2; i <= n; i++) { + for (int j = 0; j < i; j++) { + catalan[i] += catalan[j] * catalan[i - j - 1]; + } + } + + return catalan[n]; +} + +int main() { + int n; + cout << "Enter the value of n to compute nth Catalan number: "; + cin >> n; + + cout << "Catalan number C[" << n << "] = " << catalanDP(n) << endl; + return 0; +}