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