Making Change
Let's get started!
<?php //The $money variable is how much money we are starting with to break into change$money = 57.69;echo "<b> $" .$money."</b><br>";
The next thing we are going to do is take our money times 100. This means that we won't be working with any decimals. PHP tends to get a little wonky sometimes when you work with decimals, so just to be safe we'll work with whole numbers.
//We are taking the money times 100 so we arn't working with decimal points.Sometimes PHP gets weird with decimals.$money = $money *100;echo "<b> $" .$money." money times 100</b><br>";
Now we are going to start with twenty dollar bills. Remember all of our money is taken times 100 so 20 is actually 2000. We calculate how many times twenty will fully go into our sum of money. We echo back how many times that is, and proceed to take this much out of our total, giving us a new total of how much money we own the customer not including the twenty dollar bills.
//Twenty dollar bills$twe = floor ($money/2000);echo "$twe twenty dollar bills <br>";$money = $money - 2000*$twe;//End twenty dollar bills
Now we do the same code again, only for ten dollar bills. This time we switch the numbers and use 1000 inside of 2000. This is because 10 * 100 is 1000.
//Ten dollar bills$ten = floor ($money/1000);echo "$ten ten dollar bills <br>";$money = $money - 1000*$ten;//End ten dollar bills
We repeat this process over and over again (hint: loops are good at repeating) for each type of bill and coin. Always be sure that you are taking the currency times 100 before doing your calculations! Here is a breakdown of how the whole program would look:
<?php //The Money variable is how much money we are starting with to break into change$money = 57.69;echo "<b> $" .$money."</b><br>";//We are taking the money times 100 so we arn't working with decimal points.Sometimes PHP gets weird with decimals.$money = $money *100;echo "<b> $" .$money." money times 100</b><br>";//Twenty dollar bills$twe = floor ($money/2000);echo "$twe twenty dollar bills <br>";$money = $money - 2000*$twe;//End twenty dollar bills//Ten dollar bills$ten = floor ($money/1000);echo "$ten ten dollar bills <br>";$money = $money - 1000*$ten;//End ten dollar bills//Five dollar bills$five = floor ($money/500);echo "$five five dollar bills <br>";$money = $money - 500*$five;//End five dollar bills//One dollar bills$one = floor ($money/100);echo "$one one dollar bills <br>";$money = $money - $one*100;//End one dollar bills//Quarters$qua = floor ($money/25);echo "$qua quarters <br>";$money = $money - 25*$qua; //End quarters//Dimes$dime = floor ($money/10);echo "$dime dimes <br>";$money = $money - 10*$dime;//End dimes//Nickels$nic = floor ($money/05);echo "$nic nickels <br>";$money = $money - 05*$nic;//End nickels//Pennies$pen = floor ($money/01);echo "$pen pennies <br>";$money = $money - 01*$pen;//End penniesecho "<b> $" .$money." money times 100</b><br>";echo "<b> $" .($money/100)."</b><br>";?>
Running this program will give the following results:
2 twenty dollar bills
1 ten dollar bills
1 five dollar bills
2 one dollar bills
2 quarters
1 dimes
1 nickels
4 pennies
Now for the fun part, your assignment! Clean up and condense this code into a smooth running program that allows the user to enter how much change the customer should receive, and then tells them how to allocate the bills and coins. Post your answers in the blog comments here!