Skip to content

Commit

Permalink
Added program to calculate catalan number
Browse files Browse the repository at this point in the history
  • Loading branch information
chiragagarwal54 committed Oct 8, 2019
1 parent ca4c213 commit 837c6be
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions CatalanNumbers/NthCalatanNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//Program for Nth Catalan number.

#include<iostream>
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 program to test above function
int main()
{
for (int i=0; i<10; i++)
cout << catalan(i) << " ";
return 0;
}

0 comments on commit 837c6be

Please sign in to comment.