Skip to content

Commit

Permalink
Added a c++ program to find nth catalan number
Browse files Browse the repository at this point in the history
  • Loading branch information
mhdsabah committed Oct 11, 2022
1 parent b97b794 commit 91b47e3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 02. Mathematics/5. Nth_catalan_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
using namespace std;

/*
Program to find nth Catalan number
Constraints 1<=n<=19
*/
long catalan(int n)
{
long ar[n + 1];

ar[0] = 1;
ar[1] = 1;

for (int i = 2; i <= n; i++) {
ar[i] = 0;
for (int j = 0; j < i; j++)
ar[i] += ar[j] * ar[i - j - 1];
}


return ar[n];
}

int main()
{
int n;
cout<<"Enter the number ";
cin>>n;
cout<<"\n"<<n<<"th Catalan number is:"<<catalan(n)<<"\n";
return 0;
}

0 comments on commit 91b47e3

Please sign in to comment.