View Single Post
Old 02-16-2008, 04:36 AM   #7 (permalink)
SpikedCola
 
Super Techie

Join Date: Jan 2006

Location: Ontario, Canada

Posts: 456

SpikedCola is on a distinguished road

Send a message via MSN to SpikedCola
Default Re: C# Ideas - Working With Coins & Bills

My friend suggested another way of doing it: "Start with the highest denomination you want to give out, say $100s, and then go through a loop repeatedly subtracting $100 until you get a negative number, at which point you've reached the max number of hundreds. Then you move down to the next denomination."

Made perfect sense to me, so I came up with:
Code:
            double money = 0;
            string name1 = "Jordan"; // , name2 = "", name3 = "";
                                    
            Console.WriteLine("How much did " + name1 + " make?");
            Console.Write("$");
            money = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Converting {0:C} into change ...", money);

            int h = -1;
            while (money >= 0)
            {
                money = money - 100;
                h++;
            }
              
            Console.WriteLine("Hundreds: "+ h +"");
And it works perfectly! My next question, though, is how do I take what's left over before you hit a negative number, and find out how many fifties it takes to make that up?
__________________

Asus P5E | Q6600 | 2x2GB Corsair Dominator DDR2-1066 | BFG 8800GTS | 5x1.5TB SATA2 | 2x500GB SATA2 in RAID1
SpikedCola is offline