Skip to content

Commit 5fa3bcd

Browse files
authored
Merge pull request #2 from Izroth404/Izroth404-patch-2
Nth Catalan Number using JAVA
2 parents 28457a9 + 12acec9 commit 5fa3bcd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Nth_Catalan_Number.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.io.*;
2+
3+
import java.util.*;
4+
5+
//Program to calculate Nth Catalan Number using dynamic programming
6+
public class Main {
7+
8+
public static void main(String[] args) throws Exception {
9+
Scanner scn = new Scanner(System.in);
10+
11+
int n = scn.nextInt();
12+
13+
long[] dp = new long[n + 1];
14+
dp[0] = 1; //since 0th Catalan number is 1
15+
16+
dp[1] = 1; //since 1st Catalan number is also 1
17+
18+
for (int i = 2; i < dp.length; i++) {
19+
for (int j = 0; j < i; j++) {
20+
dp[i] += dp[j] * dp[i - 1 - j];
21+
}
22+
}
23+
24+
System.out.println(dp[n]); //nth Catalan number
25+
}
26+
27+
}

0 commit comments

Comments
 (0)