In: Computer Science
How can I make my php code without using the money format just loop and if statements print like this:
Change for 5 dollars and 20 cents:
0 ten dollar bills
1 five dollar bills
0 one dollar bills
0 quarters
2 dime
0 nickel
0 pennies
I am trying to ask the user to enter amount and output their change they entered.
$input;
$TenBills = 1000;
$FiveBills = 500;
$OneBills = 100;
$Quarters = 25;
$Dimes = 10;
$Nickels = 5;
$Pennies = 1;
//$dollars = $TenBills , $FiveBills , $OneBills;
$input = readline("Enter Dollars:\n");
print "Change for $input cents:\n";
if($input >= $TenBills) {
$TenBills = $input/$TenBills;
$input /= $TenBills * $TenBills;
print " $input ten dollar bills\n";
}
if($input = $FiveBills) {
$FiveBills = $input/$FiveBills;
$input /= $FiveBills * $FiveBills;
print " $input five dollar bills\n";
}
if($input = $OneBills) {
$OneBills = $input/$OneBills;
$input /= $OneBills * $OneBills;
print " $input one dollar bills\n";
}
if($input = $Quarters) {
$Quarters = $input/$Quarters;
$input /= $Quarters * $Quarters;
print " $input quarters dollar bills\n";
}
if($input = $Dimes) {
$Dimes = $input/$Dimes;
$input /= $Dimes * $Dimes;
print " $input dimes dollar bills\n";
}
if($input = $Nickels) {
$Nickels = $input/$Nickels;
$input /= $Nickels * $Nickels;
print " $input nickels dollar bills\n";
}
if($input = $Pennies) {
$Pennies = $input/$Pennies;
$input /= $Pennies * $Pennies;
print " $input pennies dollar bills\n";
}
?>
What I understand from problem statement is that the user will enter the amount and the program will calculate the change.
The program flow is :
1. Check whether the input amount is greater then Ten bills
2. if yes, then calculate the number of ten bills
3. calculate the remaining amount after calculation of ten bills
4. Print the number of ten bills
5. reset the count to zero
The above flow is same for all the other denomination (i.e. 5 bills, one bills, quarter, dime, nickel, penny)
Code:
-------------------------------------------------------------------------
<?php
$input;
$TenBills = 10;
$FiveBills = 5;
$OneBills = 1.0;
$Quarters = 0.25;
$Dimes = 0.1;
$Nickels = 0.05;
$Pennies = 0.01;
$count=0;
$input = readline("Enter Dollar (Example - 13 Dollar 31 Cent as
13.31):\n");
print "Change for $input: \n";
$input = readline("Enter Dollar (For 13 dollar 34 cent enter
13.34):\n");
print "Change for $input: \n";
if($input >= $TenBills) {
$count = floor($input/$TenBills);
$input -= $TenBills*$count;
}
print " $count ten dollar bills\n";
$count=0;
if($input >= $FiveBills) {
$count = floor($input/$FiveBills);
$input -= $FiveBills*$count;
}
print " $count five dollar bills\n";
$count=0;
if($input >= $OneBills) {
$count = floor($input);
$input -= $OneBills*$count;
}
print " $count one dollar bills\n";
$count=0;
if($input >= $Quarters) {
$count = floor($input/$Quarters);
$input -= $Quarters*$count;
}
print " $count quarters dollar bills\n";
$count=0;
if($input >= $Dimes) {
$count = floor($input/$Dimes);
$input -= $Dimes*$count;
}
print " $count dimes dollar bills\n";
$count=0;
if($input >= $Nickels) {
$count = floor($input/$Nickels);
$input -= $Nickels * $count;
}
print " $count nickels dollar bills\n";
$count=0;
if($input >= $Pennies) {
$count = floor($input/$Pennies);
$input -= $Pennies * $count;
}
print " $count pennies dollar bills\n";
$count=0;
?>
----------------------------------------------------------------------------------------
Screenshot of the Program:
Sample Output:
Change for 14.31: 1 ten dollar bills 0 five dollar bills 4 one dollar bills 1 quarters dollar bills 0 dimes dollar bills 1 nickels dollar bills 1 pennies dollar bills
Change for 5.2: 0 ten dollar bills 1 five dollar bills 0 one dollar bills 0 quarters dollar bills 2 dimes dollar bills 0 nickels dollar bills 0 pennies dollar bills