Question

In: Computer Science

How can I make my php code without using the money format just loop and if...

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";

}




?>


Solutions

Expert Solution

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

Related Solutions

How would I structure the following PHP (PDO) code into a table format using only PHP?...
How would I structure the following PHP (PDO) code into a table format using only PHP? //Our SQL statement, which will select a list of tables from the current MySQL database. $sql = "SELECT * FROM jobs"; //Prepare our SQL statement, $statement = $pdo->prepare($sql); //Execute the statement. $statement->execute(); //Fetch the rows from our statement. $tables = $statement->fetchAll(PDO::FETCH_NUM); //Loop through our table names. foreach($tables as $table){ //Print the table name out onto the page. echo $table[0], ' '; echo $table[1], '...
PHP Language I have XAMPP and I don't know how to display my PHP code. For...
PHP Language I have XAMPP and I don't know how to display my PHP code. For instance, when I create a basic HTML webpage I just open the code into a web browser and it shows me a basic web page. When I try to show a PHP calculation it doesn't show. What are the steps in displaying my PHP doc?
In which folder of XAMPP would I put in my PHP code
In which folder of XAMPP would I put in my PHP code
How can I refactor my current code to Reverse engineer an unknown file format containing the...
How can I refactor my current code to Reverse engineer an unknown file format containing the same data fields but stored using a different representation in C. My Code: #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{    double slope;    char spade;    int water;    short wood;    char afternoon;    float daughter;    unsigned long beginner;    char scissors;    char structure;    char competition;    char force;    char mark[11];    short copy;    double grain;...
could anyone make a php code for a cart page for an e commerce website?? just...
could anyone make a php code for a cart page for an e commerce website?? just the cart page not the whole website
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
How can I make a PHP web app that will record student's names and grades? Req....
How can I make a PHP web app that will record student's names and grades? Req. A new table in our database called students. Table should include 2 columns; student_name and student_grade [0 - 100]. Need 3 PHP called student_list.php, add_student.php, and delete_student.php. The student_list.php code uses HTML and PHP to list all of the students in your student's table; including their student_name and student_grade [number grade] and letter grade. Convert the number grade to a letter grade as follows....
I want the code in the following three PHP files without any CSS. Thank you Create...
I want the code in the following three PHP files without any CSS. Thank you Create a Input.php. Create a PHP script that will perform the following tasks: 1. Divide the content area of this page into two sections (Side by Side) 2. On the left side: create a form that accepts the following information from the user a. Text box, Employee Name b. Text box, Employee ID c. Text box, Telephone Number d. Text box, Email Address e. Radio...
Using PHP and MYSQL and with a simple customer database, how can I create a simple...
Using PHP and MYSQL and with a simple customer database, how can I create a simple log in and registration system for an ecommerce site
C++ Code! This code was written/implemented using the "class format." How would I go about in...
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?" #include <iostream> #include <iomanip> using namespace std; class ListNode{ public: string course_name; string course_number; string course_room; ListNode* next; ListNode(){ this->next = NULL; } ListNode(string name, string number, string room){ this->course_name = name; this->course_number = number; this->course_room = room; this->next = NULL; } }; class List{ public: ListNode* head; List(){ this->head = NULL; } void insert(ListNode* Node){ if(head==NULL){ head...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT