-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinomial.cpp
More file actions
38 lines (34 loc) · 829 Bytes
/
binomial.cpp
File metadata and controls
38 lines (34 loc) · 829 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
29
30
31
32
33
34
35
36
37
38
#include<iostream>
using namespace std;
int min(int a, int b){
return (a < b) ? a : b ;
}
// int binomial(int n, int r){
// int matrix[n+1][r+1];
// for(int i = 0; i <= n; i++){
// for(int j = 0; j <= min(i,r); j++){
// if(j == 0 || i == j){
// matrix [i][j] = 1;
// }
// else{
// matrix [i][j] = matrix[i-1][j-1] + matrix[i-1][j];
// }
// }
// }
// return matrix[n][r];
// }
int binomial(int n, int k){
if(k > n){
return 0;
}
if(n == k || k == 0){
return 1;
}
return binomial(n-1,k-1) + binomial(n-1,k);
}
int main(){
int n,r;
cout << "Enter the n and r for nCr :";
cin >> n >> r;
cout << "Value of " << n << "C" << r << " is " << binomial(n,r) << endl;
}