computing the number of combinations

what is wrong with the code it computes the result false

Code:

`/* computes the number of combinations of n items taken r at a time by the formula`

c(n,r)= n! / (r!(n-r)!), functions used are unsigned int no_of_combinations(unsiged int,

unsigend int) and unsigned int factorial ( unsigned int) */

#include<stdio.h>

void main()

{

/* function declarations */

unsigned int no_of_combinations( unsigned int, unsigned int);

/* variable declaration */

unsigned x,y,z;

char right_no = 'a';

/* function body */

while ( right_no == 'a' )

{

printf("\nEnter a number for the total number of items: ");

scanf("%d",&x);

printf("Enter the number of groups taken at a time: ");

scanf("%d",&y);

if ( y<=x )

{

z = no_of_combinations(x,y);

right_no = 'b';

}

/* end if */

}

/* end while */

printf("The number of combinations of %d taken %d at time is %d",x,y,z);

}

/* end function main */

/* function int factorial(int) */

unsigned int factorial(

/* input */ unsigned int num )

{

/* variable declarations */

unsigned int fact = 1;

unsigned int count = 2;

while ( count <= num )

{

fact = fact * count;

count++;

}

/* end while */

return fact;

}

/* end function int factorial(int) */

unsigned int no_of_combinations(

/* input */ unsigned int x, unsigned int y )

{

/* variable declaration */

unsigned int ans, fact_of_n_minus_r;

unsigned int fact_of_n, fact_of_r;

/* function declaration */

unsigned int factorial( unsigned int);

/* function body */

fact_of_n = factorial(x);

fact_of_r = factorial(y);

fact_of_n_minus_r = factorial(x-y);

ans = fact_of_n / (fact_of_r * fact_of_n_minus_r);

return ans;

}

/* end function no_of_combinations */

the non recursive version

So, I got this non recursive version where you **do not have to compute the factorials** at all.

Code:

`#include <stdio.h>`

unsigned long

choose(unsigned long n, unsigned long r)/*recursive version*/

{

if( 2*r > n) r = n-r;/*c(n,r)=c(n,n-r)*/

if( (r<0) || (r>n) ) return 0;/*error*/

else if(r==0) return 1;

else if(r==1) return n;

else

{

unsigned long t;

t=choose(n-1,r-1);

return (n*t)/r;/*c(n,r)=(n*c(n-1,r-1))/r*/

}

}

unsigned long

choose1(unsigned long n, unsigned long r) /*non recursive version*/

{

unsigned long comb=1,num,den;

if( 2*r>n) r=n-r;

if( (r<0) || (r>n) ) return 0;/*error*/

for(num=n-r,den=0;num<n;num++,den++)

{

comb = ( (num+1)*comb )/(den+1);

}

return comb;

}

int

main(void)

{

unsigned long n,i;

printf("Give n\n");

scanf("%lu",&n);

printf("n=%lu\n",n);

for(i=0;i<=n;i++)

{

printf("choose(%lu,%lu)=%lu\n",n,i,choose(n,i));

printf("choose1(%lu,%lu)=%lu\n",n,i,choose1(n,i));

}

return 0;

}