// // lotto2.cpp // CSC 215 // John Rogers // // This program computes the odds of // winning the lottery. // #include long factorial(long n); long falling(long n, long k); int main() { long numberOfBalls; long numberChosen; long odds; cout << "How many balls are in the game? "; cin >> numberOfBalls; cout << "How many balls are chosen from that? "; cin >> numberChosen; odds = falling(numberOfBalls, numberChosen) / factorial(numberChosen); cout << "The odds of winning are " << odds << ":1." << endl; return 0; } // // This computes n!. // long factorial(long n) { long index; long answer = 1; for (index = 1; index <= n; index++) { answer *= index; } return answer; } // // This computes n to the falling k power // In mathematical text, we write // "n to the falling k power" as n^k_. Its // definition is: // // n^k_ = n*(n-1)*(n-2)*...*(n-k+1) // long falling(long n, long k) { long index; long answer = 1; for (index = n-k+1; index <= n; index++) { answer *= index; } return answer; }