SOURCE FILE: nChooseC.cpp


/*

A program that calculates N choose K using a very inefficient recursive
method.  This program is about the same as nChooseA.cpp, except the recursive
function tells you what it's parameters are each time it is called, to make it
more clear what is the depth of the recursion and repetitiveness of the
calculation.

*/

#include <iostream.h>

int NchooseK(int N, int K);

int main ()
{
  int N , K , ans;

  cout << endl ;
  cout << "Give values for N and K, and this program will output"  << endl ;
  cout << "the value of N choose K: " ;
  cin >> N >> K ;
  cout << endl ;

  ans = NchooseK(N,K) ;

  cout << endl ;
  cout << "The answer is: " << ans << "." << endl ;  
  return 0;
}

int NchooseK(int N, int K)
{
  cout << "Now computing " << N << " choose " << K << "." << endl  ;
  if ( (N==K) || (0==K) ) return 1;
  else return ( NchooseK(N-1, K-1) + NchooseK(N-1,K) ) ;
}