lotto.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // lotto.cpp -- probability of winning
  2. #include <iostream>
  3. // Note: some implementations require double instead of long double
  4. long double probability(unsigned numbers, unsigned picks);
  5. int main()
  6. {
  7. using namespace std;
  8. double total, choices;
  9. cout << "Enter the total number of choices on the game card and\n"
  10. "the number of picks allowed:\n";
  11. while ((cin >> total >> choices) && choices <= total)
  12. {
  13. cout << "You have one chance in ";
  14. cout << probability(total, choices); // compute the odds
  15. cout << " of winning.\n";
  16. cout << "Next two numbers (q to quit): ";
  17. }
  18. cout << "bye\n";
  19. // cin.get();
  20. // cin.get();
  21. return 0;
  22. }
  23. // the following function calculates the probability of picking picks
  24. // numbers correctly from numbers choices
  25. long double probability(unsigned numbers, unsigned picks)
  26. {
  27. long double result = 1.0; // here come some local variables
  28. long double n;
  29. unsigned p;
  30. for (n = numbers, p = picks; p > 0; n--, p--)
  31. result = result * n / p ;
  32. return result;
  33. }