C++ Arithmetic Operator Dividing a cash amount into quarters, nickels, dimes and cents

Description

C++ Arithmetic Operator Dividing a cash amount into quarters, nickels, dimes and cents

#include <iostream>

using std::cin;/*from  w  ww . ja va  2 s  . c o m*/
using std::cout;
using std::endl;

int main() {
  const int quarter = 25;
  const int dime = 10;
  const int nickel = 5;

  double amount = 123.123;
  int amountInCents = 0;

  int quarters = 0;
  int dimes = 0;
  int nickels = 0;
  int cents = 0;


  if ((amount > 0.0) && (amount <= 10.0)) {
    amountInCents = amount * 100.0+0.5;

    // Find the number of quarters
    quarters = amountInCents/quarter;
    amountInCents %= quarter;        // Get the remainder

    // Find the number of dimes
    dimes = amountInCents / dime;
    amountInCents %= dime;           // Get the remainder

    // Find the number of nickels
    nickels = amountInCents / nickel;
    amountInCents %= nickel;         // Get the remainder

    // Find the number of cents
    cents = amountInCents;           // The remainder is already in cents

    cout << endl
         << "The dollar value $" << amount << " can be broken down into:"  << endl
         << quarters << " quarter" << ((1 == quarters) ? "," : "s,") << endl
         << dimes    << " dime"    << ((1 == dimes)    ? "," : "s,") << endl
         << nickels  << " nickel"  << ((1 == nickels)  ? "," : "s,") << endl
         << cents    << " cent"    << ((1 == cents)    ? "." : "s.") << endl;
  }
  else
    cout << endl << "You did not enter a dollar amount between 0 and 10." << endl;

  return 0;
}



PreviousNext

Related