Search code examples
.netvb.netcoin-change

how do you calculate the minimum-coin change for transaction?


Hey everyone. I have a question. I am working on Visual Basic Express and I am supposed to calculate the change from a transaction.

Now what code would I use? I have it partly working but its starting to get a little confusing.

Thank you.

For you guys who wanted more information:

Say I have one dollar and I go to the store to purchase something. I have to ask the user to put in the amount they spent and then calculate the change and print to the screen.

Then I am supposed to use the least number of quarters, dimes nickels and pennies and print it to screen.

Any help would be greatly appreciated.


Solution

  • I'm going to go out on a limb here and assume the OP is talking about change as in money returned from a transaction.

    If that's the case, then it's probably homework, so pseudo-code only.

    The simplest first-attempt way of doing it is as follows. Let cost be the cost of the transaction and tendered be the amount of money handed over (both in cents), and let's further assume your economy only has dollar bills, quarters and pennies (to make my code smaller).

    change = tendered - cost
    
    if change < 0:
        print "Pay up some more cash, cheapskate!"
        stop
    
    dollars = 0
    quarters = 0
    cents = 0
    
    while change >= 100:
        dollars = dollars + 1
        change = change - 100
    
    while change >= 25:
        quarters = quarters + 1
        change = change - 25
    
    while change >= 1:
        cents = cents + 1
        change = change - 1
    
    print dollars " dollar(s), " quarters " quarter(s), and " cents " cent(s)."
    

    Now this can no doubt be made more efficient with the use of modulo and divide operators but I leave that as an exercise for the reader.


    My suggestion is to sit down with a pencil and a bit of paper with the following columns (for handing over ten dollars for a two-dollar-and-ninety-three cent purchase):

    tendered      cost    change   dollars  quarters     cents
    --------  --------  --------  --------  --------  --------
        1000       293
    

    and run through the code line by line in your head, using the current values from the paper and writing down the new values where they change.

    This will greatly assist your understanding.


    In response to your update:

    I have one dollar and I go to the store to purchase something. I have to ask the user to put in the amount they spent and then calculate the change and print to the screen. Then I am supposed to use the least number of quarters, dimes, nickels and pennies then print it to screen.

    That's remarkably similar to what I had above:

    tendered = 100
    input cost
    cost = int (cost * 100)
    change = tendered - cost
    if change < 0:
        print "Pay up some more cash, cheapskate!"
        stop
    print "Change is ", (format "$9.99", change / 100)
    
    quarters = 0, dimes = 0, nickels = 0, pennies = 0
    
    while change >= 25:
        quarters = quarters + 1
        change = change - 25
    
    while change >= 10:
        dimes = dimes + 1
        change = change - 10
    
    while change >= 5:
        nickels = nickels + 1
        change = change - 5
    
    while change >= 1:
        pennies = pennies + 1
        change = change - 1
    
    print quarters, " quarters"
    print dimes   , " dimes"
    print nickels , " quarters"
    print pennies , " pennies"