Skip to content

Commit 9a1c665

Browse files
catalan number in cpp
1 parent d276295 commit 9a1c665

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

catalan.cpp

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
#include<iostream>
2-
using namespace std;
3-
4-
// A recursive function to find nth catalan number
5-
unsigned long int catalan(unsigned int n)
6-
{
7-
if (n <= 1) return 1;
8-
9-
// catalan(n) is sum of catalan(i)*catalan(n-i-1)
10-
unsigned long int res = 0;
11-
for (int i=0; i<n; i++)
12-
res += catalan(i)*catalan(n-i-1);
13-
14-
return res;
15-
}
16-
17-
int main()
18-
{
19-
for (int i=0; i<10; i++)
20-
cout << catalan(i) << " ";
21-
return 0;
22-
}
1+
2+
#include<iostream>
3+
using namespace std;
4+
5+
// A recursive function to find nth catalan number
6+
unsigned long int catalan(unsigned int n)
7+
{
8+
// Base case
9+
if (n <= 1) return 1;
10+
11+
// catalan(n) is sum of catalan(i)*catalan(n-i-1)
12+
unsigned long int res = 0;
13+
for (int i=0; i<n; i++)
14+
res += catalan(i)*catalan(n-i-1);
15+
16+
return res;
17+
}
18+
19+
// Driver program to test above function
20+
int main()
21+
{
22+
for (int i=0; i<10; i++)
23+
cout << catalan(i) << " ";
24+
return 0;
25+
}
26+

0 commit comments

Comments
 (0)