Bills dispenser machine I have just started out learning C++ and I am trying to write out a simple bills' dispenser machine as an exercise.

Upon user input, the system is to check if it can pay out the amount but only in 20 and 50 bills, and it should return with the minimum number of bills.

I need someone to point out and tell me if the following code I have wrote if it is viable (it seems to be working when I tested it myself, but that is too one sided). What about the structure?

There are 2 parts that I did - the first code simply by extending the main function while the latter is done by introducing the functions.

#include &lt;iostream&gt;
using namespace std;

int main()
{
    int inputAmt;

    cout &lt;&lt; "This machine only dispense in $20 and $50 bills only" &lt;&lt; endl;
    cout &lt;&lt; "Enter in the amount to be withdraw : " &lt;&lt; endl;
    cin &gt;&gt; inputAmt;

    // Decides the number of $50 bills to be dispense
    int fiftyBill;
    fiftyBill = inputAmt/50;

    int remainderAmt;
    remainderAmt = inputAmt - (fiftyBill*50);

    // Test the Remainder Amount if it is even
    int remE;
    remE = remainderAmt % 20;

    if (remE &gt;= 10 || remE &lt;= 20 )
    {
        // If it is not, reduced $50 by 1 and add it to the remainder amount
        fiftyBill = fiftyBill - 1;
        remainderAmt = remainderAmt + 50;
    }

    // Decides the number of $20 bills to be dispense
    int twentyBill;
    twentyBill = remainderAmt / 20;

    // Output Message
    cout &lt;&lt; "Number of $50 bills : " &lt;&lt; fiftyBill &lt;&lt; endl;
    cout &lt;&lt; "Number of $20 bills : " &lt;&lt; twentyBill &lt;&lt; endl &lt;&lt; endl;

    cout &lt;&lt; "Number of bills dispensed is " &lt;&lt; fiftyBill + twentyBill &lt;&lt; endl;

    int leftoverAmt;
    leftoverAmt = inputAmt - (fiftyBill * 50) - (twentyBill * 20);
    if (leftoverAmt != 0)
    {
        cout &lt;&lt; "There are a leftover of $" &lt;&lt; leftoverAmt &lt;&lt; " that cannot be dispensed." &lt;&lt; endl;
    }
}


Lastly, I tried to integrate 2 functions bool dispense (int amt) (returns true if the amount indicated can be dispensed in combinations of 20 and 50 bills, false otherwise) and int dispenseFif (int amt) (returns the number of 50 bills to dispense that will results in least number of notes being dispensed).

#include &lt;iostream&gt;
using namespace std;

bool canDispense (unsigned int amount);
int dispense50s (unsigned int amount);

int main()
{
    int inputAmt;
    cout &lt;&lt; "This machine only dispense in $20 and $50 bills only" &lt;&lt; endl;
    cout &lt;&lt; "Enter in the amount to be withdraw : " &lt;&lt; endl;
    cin &gt;&gt; inputAmt;

    canDispense(inputAmt);
}

bool canDispense (unsigned int amount)
{    
    int fifAmt = dispense50s(amount);
    int remainderAmt;
    remainderAmt = amount - (fifAmt*50);

    // Test the Remainder Amount if it is even
    int remE;
    remE = remainderAmt % 20;
    cout &lt;&lt; "remE : " &lt;&lt; remE &lt;&lt; endl &lt;&lt; endl;

    if (remE &gt;= 10 || remE &gt;= 20 )
    {
        fifAmt = fifAmt - 1;
        remainderAmt = remainderAmt + 50;
    }

    // Decides the number of $20 bills to be dispense
    int twentyBill;
    twentyBill = remainderAmt / 20;

    int leftoverAmt;
    leftoverAmt = amount - (fifAmt * 50) - (twentyBill * 20);

    if (leftoverAmt == 0)
        {
            // Output Message
            cout &lt;&lt; "(2) Number of $50 bills : " &lt;&lt; fifAmt &lt;&lt; endl;
            cout &lt;&lt; "(2) Number of $20 bills : " &lt;&lt; twentyBill &lt;&lt; endl;

            cout &lt;&lt; "(2) Number of bills dispensed is " &lt;&lt; fifAmt + twentyBill &lt;&lt; endl; 
            return true;
        }
    else
        {
        cout &lt;&lt; "The amount is impossible! Please try again... " &lt;&lt; endl;
        return false;
        }

}

int dispense50s (unsigned int amount)
{
    // Decides the number of $50 bills to be dispense
    int fiftyBill;
    fiftyBill = amount/50;
    return fiftyBill;
}
This is a tricky problem to solve.  In the general case, the change-making-problem requires dynamic-programming to solve.  It is possible to write a simpler algorithm that works for $20 and $50 only, but yours is buggy:

This machine only dispense in $20 and $50 bills only
Enter in the amount to be withdraw : 
30
remE : 10

(2) Number of $50 bills : -1
(2) Number of $20 bills : 4
(2) Number of bills dispensed is 3


Furthermore, if (remE &gt;= 10 || remE &gt;= 20 ) in the second solution looks wrong.



If I had to write a quick-and-dirty non-generalized solution for $50 and $20, I would write it this way:

#include &lt;cassert&gt;
#include &lt;iostream&gt;
#include &lt;iomanip&gt;

/**
 * Composes the given amount as a number of 50-dollar bills and 20-dollar
 * bills (as out-parameters).  Returns the total number of bills needed, or -1
 * if the amount cannot be formed.
 */
int split_50_20_bills(int amount, int *num_50, int *num_20) {
    if (amount &lt; 0 || amount == 10 || amount == 30 || amount % 10) {
        return -1;
    } else {
        *num_50 = (amount % 20) ? ((amount - 50) / 100) * 2 + 1 :
                                  (amount / 100) * 2;
        *num_20 = (amount - 50 * *num_50) / 20;
        assert(amount == 50 * *num_50 + 20 * *num_20);
        return *num_50 + *num_20;
    }
}

int main()
{
    for (int amt = 0; amt &lt;= 1000; amt += 10) {
        int num_50, num_20;
        int total = split_50_20_bills(amt, &amp;num_50, &amp;num_20);
        std::cout &lt;&lt; std::setw(4) &lt;&lt; amt;
        if (total &lt; 0) {
            std::cout &lt;&lt; " cannot be formed\n";
        } else {
            std::cout &lt;&lt; " = " &lt;&lt; std::setw(2) &lt;&lt; num_50 &lt;&lt; " x $50 + "
                               &lt;&lt; std::setw(2) &lt;&lt; num_20 &lt;&lt; " x $20 ("
                               &lt;&lt; std::setw(2) &lt;&lt; total &lt;&lt; " bills)\n";
        }
    }
}


Note that one function passes back four pieces of information:


Whether it's possible to form the amount
If so, how many bills it will take altogether
How many $50 bills
How many $20 bills