fork download
  1. #include <iostream>
  2.  
  3. int main() {
  4. const int DOLLAR = 100 , HALF_DOLLAR = 50 ,
  5. QUARTER = 25 , DIME = 10 , NICKEL = 5 ,
  6. PENNY = 1;
  7. int price = 0 , paid = 0; //int not double
  8. int number_dollar = 0 , number_half = 0 ,
  9. number_quarter = 0 , number_dime = 0 ,
  10. number_nickel = 0 , number_penny = 0 ,
  11. change = 0;
  12.  
  13. std::cout << "Please enter the price(cents): ";
  14. std::cin >> price;
  15. std::cout << "Please enter what you paid(cents): ";
  16. std::cin >> paid;
  17.  
  18. change = paid - price; //dont need to convert to dollars anymore
  19.  
  20. number_dollar = change / DOLLAR;
  21. change %= DOLLAR;
  22.  
  23. number_half = change / HALF_DOLLAR;
  24. change %= HALF_DOLLAR;
  25.  
  26. number_quarter = change / QUARTER;
  27. change %= QUARTER;
  28.  
  29. number_dime = change / DIME;
  30. change %= DIME;
  31.  
  32. number_nickel = change / NICKEL;
  33. change %= NICKEL;
  34.  
  35. number_penny = change / PENNY;
  36. change %= PENNY;
  37.  
  38. std::cout << "Dollars - " << number_dollar << std::endl
  39. << "Half - " << number_half << std::endl
  40. << "Quarter - " << number_quarter<< std::endl
  41. << "Dime - " << number_dime << std::endl
  42. << "Nickel - " << number_nickel << std::endl
  43. << "Penny - " << number_penny << std::endl;
  44. }
Success #stdin #stdout 0s 3300KB
stdin
3401
4000
stdout
Please enter the price(cents): Please enter what you paid(cents): Dollars - 5
Half    - 1
Quarter - 1
Dime    - 2
Nickel  - 0
Penny   - 4