-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalanNumber.java
More file actions
25 lines (22 loc) · 796 Bytes
/
CatalanNumber.java
File metadata and controls
25 lines (22 loc) · 796 Bytes
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
import java.util.*;
import java.math.BigInteger;
class CatalanNumber {
public int n = 0;
public BigInteger C = BigInteger.ONE;
public BigInteger next() {
n++;
C = C.multiply(BigInteger.valueOf(4 * n - 2)).divide(BigInteger.valueOf(n + 1));
return C;
}
public static void main(String args[]) {
System.out.println("This program finds the first n Catalan numbers.");
System.out.print("Enter n :\t");
try {
int n = new Scanner(System.in).nextInt();
for (CatalanNumber C = new CatalanNumber(); C.n < n; C.next())
System.out.printf("Cat(%,d)\t=%, d\n", C.n, C.C);
} catch (InputMismatchException e) {
System.out.println("Please enter a valid integer.");
}
}
}