Thread: Make Change

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    36
    Rep Power
    12

    Make Change


    I am to write a program in C++ that will take a dollar and cents amount, and break it down and tell how many dollars, halves, quarters, dimes, nickels, and pennies are used in that amount. Now I ask for some help on this. This is a program I am to write, so of course I do not want everything done for me. But I would like a few hints and whatnot here and there. I would very much appreciate that. Thank you.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    You probably want to use the / and % operators here. Note that when using int variables, the / operator performs integer division (and thus rounds down automatically). This is how I would do it.

    1. Input a value from the user as dollars and cents into a float variable.
    2. Convert the dollars and cents to cents alone and assign it to an int variable.
    3. Divide (i.e. use /) the int variable by 100 to get the number of dollars to use.
    4. Modulo divide (i.e. use %) the int variable by 100 to get the remainder.
    5. Divide the number from step 4 by 50 to get the number of half-dollars to use
    6. Modulo divide the result from step 4 by 50 to get the remainder
    7. Divide the number from step 6 by 25 to get the number of quarters
    8. Modulo divide the result from step 6 by 25 to get the remainder
    9. Continue the same steps for dimes, nickels etc.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    My mother worked for the post office before she married. Since they didn't have anything to calculate the change for them, all clerks had learned a simple and reliable way of giving change that the customer could also follow very easily. And she taught it to me. And now I will teach it to you.

    1. Start with the amount of the purchase.
    2. Count out loud up to the amount the customer paid with, skipping those steps that do not apply:
    a. With pennies, count up to the nearest nickel (ie, to the amount of cents evenly divisible by five).
    b. With nickels, count up to the nearest dime.
    c. With dimes, count up to the nearest quarter.
    d. With quarters, count up to the nearest dollar.
    e. With dollars, count up to the nearest five dollars.
    f. And so on until you arrive at the amount paid.

    Of course, steps 2b and 2c could be reversed, depending on which will get you to that quarter. And if you don't have enough of a particular coin, then you would need to substitute others. But the basic procedure is there and works very well.

    Example:
    The bill is $3.52 and you pay with twenty-dollar bill --
    The bill: "$3.52"
    With pennies: "53, 54, 55" 3 pennies
    To get to the next quarter will take dimes, so we skip the nickels.
    With dimes: "65, 75" 2 dimes
    With quarters: "$4.00" 1 quarter
    With ones: "$5.00" 1 one
    With fives: "$10.00" 1 five
    With tens: "$20.00" 1 ten
    Change was $16.48 in 1 ten, 1 five, 1 one, 1 quarter, 2 dimes, and 3 pennies.

    Giving change used to be a basic skill that every cashier needed to know, but now it's a lost art. I've been in a store (ironically in a computer store) when the power when out and it would have been hilarious to watch the clerks struggle to make change if it weren't wasting so much time. They were even having trouble figuring out the sales tax with hand-held calculators; I guess they no longer have a tax table to look it up on.
    Last edited by dwise1_aol; September 22nd, 2003 at 02:31 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    NC, USA
    Posts
    364
    Rep Power
    13
    Here's a coin calculator that I wrote in Python. You can use the same logic as I did.

    Code:
    #!/usr/bin/env python
    
    coins = int(raw_input("Enter an amount less than $1.00: "))
    print coins, "coins"
    
    #quarters
    quarters = coins / 25
    print quarters, "quarters"
    coins = coins % 25
    
    #dimes
    if coins / 10 < 1:
    	dimes = 0
    else:
    	dimes = coins / 10 
    print dimes, "dimes"
    coins = coins % 10
    
    #nickels
    if coins / 5 < 1:
    	nickels = 0
    else:
    	nickels = coins / 10
    print nickels, "nickels"
    coins = coins % 5
    
    #pennies
    pennies = coins / 1
    print pennies, "pennies"

IMN logo majestic logo threadwatch logo seochat tools logo