Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* factorial */
  4. int fact(int n){
  5.     if(n == 0){
  6.         return 1;
  7.     } else {
  8.         return n * fact(n-1);
  9.     }
  10. }
  11.  
  12. /* combination */
  13. int comb(int n, int r){
  14.     if(n == r){
  15.         return 1;
  16.     }
  17.     if(r >= n){
  18.         printf("please enter n > r > 0 \n");
  19.         return 0;
  20.     }
  21.  
  22.     return fact(n) / (fact(r) * fact(n-r));
  23. }
  24.  
  25. int main(){
  26.     // test case 1, di bawah 12
  27.     printf("case 1 = %d\n", comb(8, 4));
  28.  
  29.     // test case 2, di atas 12
  30.     printf("case 2 = %\n", comb(17,13));
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement