fork download
  1. // A naive recursive C++ implementation
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // Returns value of Binomial Coefficient C(n, k)
  6. int binomialCoeff(int n, int k)
  7. {
  8. // Base Cases
  9. if (k > n)
  10. return 0;
  11. if (k == 0 || k == n)
  12. return 1;
  13.  
  14. // Recur
  15. return binomialCoeff(n - 1, k - 1)
  16. + binomialCoeff(n - 1, k);
  17. }
  18.  
  19. /* Driver code*/
  20. int main()
  21. {
  22. int n = 5, k = 2;
  23. cout << "Value of C(" << n << ", " << k << ") is "
  24. << binomialCoeff(n, k);
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0.01s 5388KB
stdin
 5 2
stdout
Value of C(5, 2) is 10