Skip to content

Commit dee89f3

Browse files
committed
Catalan code added
1 parent 2eed529 commit dee89f3

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Diff for: C-Sharp/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ _add list here_
4848

4949
- [Finding no of digits in an integer](math/Finding_no_of_digits_in_an_integer.cs)
5050
- [Tower of Hanoi](math/tower_of_hanoi.cs)
51+
- [Catalan Number](math/Catalan_Number.cs)
5152

5253
## Other
5354

Diff for: C-Sharp/math/Catalan_Number.cs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Nth Catalan Number
2+
using System;
3+
class CatalanNumber
4+
{
5+
// A recursive function to find
6+
// nth catalan number
7+
static int catalan(int n)
8+
{
9+
int res = 0;
10+
11+
// Base case
12+
if (n <= 1)
13+
{
14+
return 1;
15+
}
16+
for (int i = 0; i < n; i++)
17+
{
18+
res += catalan(i) * catalan(n - i - 1);
19+
}
20+
return res;
21+
}
22+
23+
// Main Function
24+
public static void Main()
25+
{
26+
int number;
27+
Console.Write("Enter the Number: ");
28+
number = int.Parse(Console.ReadLine());
29+
Console.Write("Nth Catalan numbers are: ");
30+
// Catalan Numbers
31+
for (int i = 0; i < number; i++)
32+
Console.Write(catalan(i) + " ");
33+
}
34+
}
35+
36+
/*
37+
Input:
38+
Enter the Number: 4
39+
Output:
40+
Nth Catalan numbers are: 1 1 2 5
41+
42+
Time Complexity: O(2^n)
43+
Space Complexity: O(1)
44+
*/

0 commit comments

Comments
 (0)