# Combinations

• 10-18-2004
Cmuppet
Combinations
Hi , I have to write a program that calculates the number of possible combinations k of n objects.
It has to be a recursive program.
It compiles and runs, but while debugging while I type in some numbers it returns 0 everytime

Thanks for any help

Code:

``` #include <stdio.h> int possibleCombi(int n,int k); int main (void) {   int n,k,combinations;   printf("type in n , number of total objects");   scanf("%d",&n);   printf("type in k , numbers drawn");   scanf("%d",&k);   combinations = possibleCombi(n,k);   printf("%d",combinations); } int possibleCombi(int n,int k) {         if ((k = 0) || (n = k)){                 return 1;         }         if(n > k > 0){                 return (possibleCombi(n - 1,k) + possibleCombi(n - 1,k - 1));         }         }```
• 10-19-2004
Nyda
I'm pretty sure this is not what you want:

Code:

`          if(n > k > 0){`
This evaluates to ...

Code:

`  if ((n > k) > 0) {`
which means: if the boolean result of n > k (either "true" or "false") is > 0 do....

You probably want something like

Code:

`  if ((n > k) && (k > 0)) {`
• 10-19-2004
Cmuppet
Thanks , I made that change to my code, but it still returns a zero everytime :confused:
• 10-19-2004
Scarlet7
You're assigning k and n to 0 in the if statement try:

if ((k == 0) || (n == k)){
• 10-19-2004
Cmuppet
Thanks , it works now , except that the answer isn't right I think.
Shouldn't the answer for example with n = 8 and k = 3 be 512.
Instead it returns then 56 :confused:
• 10-19-2004
viaxd
56 is correct
• 10-19-2004
sand_man
>Shouldn't the answer for example with n = 8 and k = 3 be 512
you must be a little confused.

that would be
8^3 = 512 (not XOR)
8*8*8 = 512