Pages

While working through C++ Demystified, I found a cool little code example called "The Change Machine". Here is the code:

#include <iostream>

#include <string>

using namespace std;

int main()

{

int total, dollars, quarters, dimes, nickels, leftover;

cout << "Enter number of pennies to make change for: ";

cin >> total;

dollars = total / 100;

leftover = total % 100;

quarters = leftover / 25;

leftover %= 25;

dimes = leftover / 10;

leftover %= 10;

nickels = leftover / 10;

leftover %= 5;

cout << "Dollars: " << dollars << endl;

cout << "Quarters: " << quarters << endl;

cout << "Dimes: " << dimes << endl;

cout << "Nickels: " << nickels << endl;

cout << "Pennies: " << leftover << endl;

system("pause");

return 0;

}

As the name implies, this program works by having the user input a number, then the change that could be made out for that number (in dollars) is outputted. Compile in an IDE to test it out for yourself!