// // Program name: make_change // Author: B. Massingill // // Purpose: "make change" -- i.e., convert a non-negative amount // of change into a number of quarters, dimes, nickels, and // pennies // // Input: integers X and Y representing "amount due" (in pennies) // and "amount tendered" (also in pennies), read from standard // input // Output: // the program "makes change" for X cents out of Y cents. // that is, it expresses the difference Y - X as // Q quarters // D dimes // N nickels // P pennies // with // Y - X = Q*25 + D*10 + N*5 + P*1 // and Q, D, and N non-negative and "as big as possible" // (that is, Q is as big as possible, and D is as big as // possible given Q, and so forth) // // if X or Y is not positive, or Y - X is negative, the program // prints an error message (to standard output) // otherwise it prints the values of Q, D, N, and P as above // // Example: // if Y-X = 55, Q=2, D=0, N=1, P=0 // // C++ programs begin with the following three lines #include int main() { int X, Y; int change_due; cout << "Enter amount owed, amount tendered\n" ; cin >> X; cin >> Y; if (X <=0) { cout << "Error: amount owed must be > 0\n" ; } else if (Y <= 0) { cout << "Error: amount tendered must be > 0\n" ; } else if (Y < X) { cout << "Error: amount tendered must be >= " << "amount owed\n" ; } else { change_due = Y - X; cout << "Change due is " << change_due << endl; // (Q, change_due) <- change_due/25 // print Q // note that: // Q*25 + change_due = Y-X // change_due < 25 cout << "\t" << change_due/25 << " quarters\n"; change_due = change_due % 25; // (D, change_due) <- change_due/10 // print D // note that // Q*25 + D*10 + change_due = Y-X // D <= 2 // change_due < 10 cout << "\t" << change_due/10 << " dimes\n"; change_due = change_due % 10; // (N, change_due) <- change_due/5 // print N // note that // Q*25 + D*10 + N*5 + change_due = Y-X // N <= 1 // change_due < 5 cout << "\t" << change_due/5 << " nickels\n"; change_due = change_due % 5; // print P = change_due // note that // Q*25 + D*10 + N*5 + P = Y-X // P < 5 cout << "\t" << change_due << " pennies\n"; } // C++ programs end with the following two lines return 0 ; }