// Oldham, Jeffrey D. // 1999 Oct 25 // CS1320 // // Compute the Binomial Coefficient of Two Values // The user is queried for n and k. // The value of $n \choose k$ is printed. $n$ and $k$ are swapped is // necessary. #include // Function Prototypes int combination(const int n, const int k); int factorial(const int n); void obtainInput(int& n, int& k); void swap(int& first, int& second); int main() { int n, k; obtainInput(n,k); cout << combination(n,k) << endl; return 0; } int combination(const int n, const int k) { return factorial(n) / (factorial(k) * factorial(n-k)); } int factorial(const int n) { int product = 1; int index; for (index = n; index > 0; --index) product *= index; return product; } void swap(int& first, int& second) { int temp = first; first = second; second = temp; return; } void obtainInput(int& n, int& k) { cout << "Please enter n and k for (n choose k): "; cin >> n >> k; if (n < k) swap(n,k); return; }