```
/*
* Choose.c - A program to calculate and print the value of
* the choose function C(n, r).
* This program is the model solution to Quiz #3 (take home).
* It also illustrates a fancy input technique.
*
* Written by Wayne Pollock,  2001 Tampa Florida USA.
*/

#include <stdio.h>

#define OK 1

/* P R O T O T Y P E S */

int check ( int n, int r );                /* Checks if r < n */
unsigned long choose ( int n, int r );     /* Computes C(n,r) */
unsigned long fact ( unsigned long num );  /* Computes num!   */

int main ( void )
{
int n, r;
char line[BUFSIZ], junk[BUFSIZ];

fprintf( stderr, "Enter positive integers n and r: " );
if ( fgets( line, sizeof(line), stdin ) == NULL )
{  fprintf( stderr, "*** ERROR!  End Of File encountered!\a\n" );
return -1;
}
if ( sscanf( line, "%i %i %s", &n, &r, junk ) != 2 )
{  fprintf(stderr, "*** ERROR!  You must enter exactly 2 numbers,"
" separated by white space!\a\n" );
return -2;
}
if ( check( n, r ) != OK )
return -3;
printf( "C(%i,%i) = %lu.\n", n, r, choose( n, r ) );
return 0;
}

int check ( int n, int r )
{
if ( n < 0 || r < 0 )
{  fprintf( stderr, "*** ERROR!  Numbers must be POSITIVE!\a\n" );
return !OK;
}
if ( r > n )
{  fprintf( stderr, "*** ERROR!  n must be as large or larger "
"than r!\a\n" );
return !OK;
}
return OK;
}

unsigned long fact ( unsigned long num )
{
unsigned long result;

for ( result = 1; num; --num )
result *= num;
return result;
}

unsigned long choose ( int n, int r )
{
return ( fact(n) / fact(r) / fact(n-r) ); /* Note: A/(B*C) = A/B/C */
}

/*
============================Output of Choose.c============================

Enter positive integers n and r: 5 2
C(5,2) = 10.
*/
```

 Send comments and mail to pollock@acm.org.