forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalan.c
More file actions
28 lines (28 loc) · 679 Bytes
/
Copy pathcatalan.c
File metadata and controls
28 lines (28 loc) · 679 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
26
27
28
/*
code for computing nth catalan number
*/
#include <stdio.h>
long int factorial(int x) // long int for more than 10 factorial
{
int i;
long int fac; // fac stores x factorial
fac = x;
for (i = 1; i < x; i++) // loop to calculate x factorial
{
fac = fac * (x - i);
}
return fac; // returning x factorial
}
int main()
{
long int f1, f2, f3; // long int for more than 10 factorial
int n;
float C; // C is catalan number for n;
scanf("%d", &n);
f1 = factorial(2 * n);
f2 = factorial(n + 1);
f3 = factorial(n);
C = f1 / (f2 * f3); // formula for catalan number for n
printf("%0.2f", C);
return 0;
}