File tree 2 files changed +45
-0
lines changed
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ _add list here_
48
48
49
49
- [ Finding no of digits in an integer] ( math/Finding_no_of_digits_in_an_integer.cs )
50
50
- [ Tower of Hanoi] ( math/tower_of_hanoi.cs )
51
+ - [ Catalan Number] ( math/Catalan_Number.cs )
51
52
52
53
## Other
53
54
Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments