Skip to content

Commit

Permalink
Merge pull request rathoresrikant#756 from Felix1898/master
Browse files Browse the repository at this point in the history
Added a C++ Program to Find nth Catalan Number
  • Loading branch information
rathoresrikant authored Sep 30, 2019
2 parents 118838b + 248dbf8 commit 6766f21
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Algorithms/DynamicProgramming/catalan_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#include <iostream>
using namespace std;

// A dynamic programming based function to find nth
// Catalan number
unsigned long int catalan(unsigned int n)
{
unsigned long int catlnno[n+1];
catlnno[0] = catlnno[1] = 1;
for (int i=2; i<=n; i++)
{
catlnno[i] = 0;
for (int j=0; j<i; j++)
{
catlnno[i] += catlnno[j] * catlnno[i-j-1];
}
}

return catlnno[n]; //Return nth Catalan No
}

int main()
{
int n=10;
cout << catalan(n) << " ";
return 0;
}

0 comments on commit 6766f21

Please sign in to comment.