Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,496 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,352 people online right now. Registration is fast and FREE... Join Now!




program to find change

 
Reply to this topicStart new topic

program to find change

INFIA
22 Mar, 2007 - 06:03 PM
Post #1

New D.I.C Head
*

Joined: 8 Feb, 2007
Posts: 20


My Contributions
I am currently working on a program to find the change from a business transaction. the highest amount tendered is a 50 dollar bill. I am not sure how to do this. i have tried to do this by using a strictly mathmatical approach but i always end up with remanders that will show up. the program must say how many twenties,tens,fives,ones,quarters, dimes, nickels and pennies are returned. i can't seem to get it right. i would post the code i have but it wont work even with any modifications. does any1 know a program like this or can show me an example of how to write this? thanks, INFIA
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Program To Find Change
22 Mar, 2007 - 09:09 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
If you search the forum this is a common assignment and I have seen this topic go by before (think there may even be a snippet).

I would post some links but my internet is currently being a pain.

The basic principal is this. Say I have 57$ and 67 cents that I need to give in change. I start with the largest bill and I divide (integer division) 57/20 = 2 (and some remainder), then I use the %-mod (or remainder) operator to find the remainder 57 % 20 = 17... so I know I need 2 twenties, 17 dollers and 67 cents. Well now 17/10 = 1 and 17%10 = 7, you guest it, I need 2 twenties, 1 ten, and 7 dollers and 67 cents. 7/5 = 1 and 7%5 = 2 so now I am at 2 twenties, 1 ten, 1 five, 2 dollers, and 67 cents. 67/25=2, 67%2=17...etc etc.

Now there is another way (a little less fancy) to do this. The same way most pizza delivery divers do. 57-20=37-20=17 < 20 so I need 2 twenties. 17-10=7 < 10 so I need 1 ten. 7-5=2 < 5 so I need 1 five, and 2 dollars. belive it or not these two methods are just about the same.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month