fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. int main()
  7. {
  8. const unsigned quartervalue = 25;
  9. const unsigned dimevalue = 10;
  10. const unsigned nickelvalue = 5;
  11. const unsigned pennyvalue = 1;
  12. std::cout << std::fixed << std::setprecision(2);
  13. // Get a random number from 0-100
  14. srand(time(nullptr));
  15. unsigned change = rand() % 100;
  16.  
  17. std::cout << "Change Due: $" << (change / 100.0) << "\n";
  18. std::cout << "Coin Dispenser will dispense:\n";
  19. // If change is greater than or equal to one run the program
  20. if (change >= 1) {
  21. unsigned quarter = change / quartervalue;
  22. change = change % quartervalue;
  23. unsigned dime = change / dimevalue;
  24. change = change % dimevalue;
  25. unsigned nickel = change / nickelvalue;
  26. change = change % nickelvalue;
  27. unsigned penny = change;
  28.  
  29. if (quarter != 0)
  30. std::cout << " Quarters: " << quarter << " ($" <<
  31. (quarter * quartervalue / 100.0) << ")\n";
  32. if (dime != 0)
  33. std::cout << " Dimes: " << dime << " ($" <<
  34. (dime * dimevalue / 100.0) << ")\n";
  35. if (nickel != 0)
  36. std::cout << " Nickels: " << nickel << " ($" <<
  37. (nickel * nickelvalue / 100.0) << ")\n";
  38. if (penny != 0)
  39. std::cout << " Pennies: " << penny << " ($" <<
  40. (penny * pennyvalue / 100.0) << ")\n";
  41. } else {
  42. // If change is = to 0, don't run the entire program, just run this portion.
  43. std::cout << "No Coins\n";
  44. }
  45. }
  46.  
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
Change Due:   $0.93
Coin Dispenser will dispense:
 Quarters: 3 ($0.75)
 Dimes:    1 ($0.10)
 Nickels:  1 ($0.05)
 Pennies:  3 ($0.03)