# C(n,r) calulation

Hey everybody! Hope everyone is having a good weekend thus far! I was hoping to get a wee bit of help.

So I wrote a program that calculates C(n,r). The user inputs n and r and then by using a few functions presto! Everything works fine. That is, until something like 16 and 5 are input. In which case I get some really funky numbers. It seems that when the solution of C(n,r) is below 1000 the output generated is fine. However, when the output is over 1000 something weird begins to happen. I've tried turning my functions into long int, and changing variables around to long int as well, but that doesn't seem to work either. It seems like it's an output problem because it seems like the math is right; if the math wasn't right, it would never get a right answer. Can anybody explain to me what I'm doing wrong?

Thanks everybody

Here is the code!

#include
#include

void err_msg();
void check(int r, int n);
int fact(int x);
int comb(int x, int y);

int main(void) { // beginning main function
setvbuf(stdout, NULL, _IONBF, 0);
char buffer[BUFSIZ+1];
int n, r;

do
{ // beginning of while statement while r and n are greater than 0
printf("
Please enter your value for n: (note: a negative number will exit the program)
");
gets(buffer);
n = atoi(buffer); // Holds the value of n
printf("Please enter your value for r: (note: a negative number will exit the program)
");
gets(buffer);
r = atoi(buffer); //hold the value of r
if ( (r > 0) && (n > 0) ) {
printf("Your 'n' value is: %d, while your 'r' value is: %d
", n, r);
check(r, n);
printf("The solution to C(n,r) is: %d
", comb(n, r));
}
else
printf ("End of program
");

} //ending of do statement
while ( (r > 0) && (n > 0) );

return EXIT_SUCCESS;
} // ending of main function

/***********************************************************************************************************/

void err_msg() // This function is simply used to tell the user that his/her input in invalid

{ //beginning of void function

printf("The data that has been entered is invalid. Please re-enter appropriate data.
");

}// ending of void function
/***********************************************************************************************************/
/***********************************************************************************************************/
void check(int x, int y) // This function compares the value of n and r to see if chews can be calculated
{ // beginning of check function
if (x > y) // dummy variables for r and n
err_msg(); //invoking the err_msg function

else
printf("Your data is valid, lets compute C(n,r)!
");
} //ending of void function

/***********************************************************************************************************/

int fact(x)
{ //beginning of fcuntion that computers factorial
int factorial = 1;
int counter;
for (counter = 1; counter <= x; counter = counter + 1) { //beginning of for loop
factorial = factorial * counter;
} // ending of for loop
return factorial;
}// ending of factorial function

/***********************************************************************************************************/

int comb(int x, int y) { //beginning of the comb function
int chews; // dummy variable that simply holds the result of the calculation to return to the user in main
chews = (fact(x)/(fact(y)*fact(x-y)));
return chews;
} //ending of the comb function
/***********************************************************************************************************/