Question

write a PHP program makes change for a given number of cents. The program should ask...

write a PHP program makes change for a given number of cents. The program should ask the user for the amount of cents and then output the change for specific denominations of ten dollar bills, five dollar bills, one dollar bills, quarters, dimes, nickels, and pennies.

Here is a sample program output for an input of 265 cents:

Change for 2 dollars and 65 cents :

0 ten dollar bills

0 five dollar bills

2 one dollar bills

2 quarters

1 dime

1 nicke

l 0 pennies

The PHP function readline can be used to get user input. The function can be used as follows: // prompt the user and store the result in the variable $input $input = readline (" Prompt ")

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation in the code is written in comments wherever needed.

Code:

<?php
$input = readline ("Enter amount in cents");
//$rem variable is used to calculate
$rem = $input;
//variables to store number of denominations
$dollarTen = 0; // Ten dollar is 1000 cents
$dollarFive = 0; // Five dollar is 500 cents
$dollarOne = 0; // One dollar is 100 cents
$quarters = 0; // One quarter is 25 cents
$dimes = 0; // One dime is 10 cents
$nickels = 0; // One nickel is 5 cents
$pennies = 0; // One penny is one cent
//floor() function is used to round down the nearest integer value
if (is_numeric($rem)) {
//to find the number of ten dollar
if ($rem >= 1000) {
$dollarTen = floor($rem / 1000); //using floor to get remove decimal values
$rem = $rem - ($dollarTen * 1000);
}
//to find the number of five dollar
if ($rem >= 500) {
$dollarFive = floor($rem / 500); //using floor to get remove decimal values
$rem = $rem - ($dollarFive * 500);
}
//to find the number of one dollar
if ($rem >= 100) {
$dollarOne = floor($rem / 100); //using floor to get remove decimal values
$rem = $rem - ($dollarOne * 100);
}
//to find the number of quarters
if ($rem >= 25) {
$quarters = floor($rem / 25); //using floor to get remove decimal values
$rem = $rem - ($quarters * 25);
}
//to find the number of dimes
if ($rem >= 10) {
$dimes = floor($rem / 10); //using floor to get remove decimal values
$rem = $rem - ($dimes * 10);
}
//to find the number of nickels
if ($rem >= 5) {
$nickels = floor($rem / 5); //using floor to get remove decimal values
$rem = $rem - ($nickels * 5);
}
//remaining will be the number of the pennies
$pennies = $rem;
}
//calculating the values for the first line of output
$cents = $input % 100; //using modulus operator to find the number of cents
$dollr = ($input - $cents) / 100; //to find the number of dollars
//i have used '\n' for new line, '\r\n' or nl2br() can also be used
echo "\nChange for ".$dollr." dollars and ".$cents." cents :\n";
echo $dollarTen." ten dollar bills\n";
echo $dollarFive." five dollar bills\n";
echo $dollarOne." one dollar bills\n";
echo $quarters." quarters\n";
echo $dimes." dime\n";
echo $nickels." nickel\n";
echo $pennies." pennies";

Add a comment
Know the answer?
Add Answer to:
write a PHP program makes change for a given number of cents. The program should ask...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (In Python 3) Write a program with total change amount as an integer input, and output...

    (In Python 3) Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes So...

  • All items at a "Penny Fair" are priced less than $1.00. Write a program that makes...

    All items at a "Penny Fair" are priced less than $1.00. Write a program that makes change with a minimum number of coins when a customer pays for an item with a one dollar bill. Prompt for the item price in cents, report the change due, and the number of coins of each denomination required. Use only quarters, dimes, nickels, and pennies for change (no 50 cent pieces). See sample output but your program should work for any item price....

  • Write a program called "ConvertPennies" to input an integer value representing a number of pennies and...

    Write a program called "ConvertPennies" to input an integer value representing a number of pennies and convert it to its equivalent number of Dollars, Quarters, Dimes, Nickels and Pennies. Following is the result of the execution of this program for 292 pennies. Enter the amount of pennies to convert: 292 pennies is equal to 2 one dollar bills 3 quarters 1 dimes 1 nickels 2 pennies Have a nice day! This is given: //********************************************************************** // Programmer: Your first and last...

  • C++ Write a program that helps a young student learn to make change. Use the random...

    C++ Write a program that helps a young student learn to make change. Use the random number generator to generate change amounts between1¢ and 99¢. Ask the user how many quarters, dimes, nickels and pennies they need to make that amount. Determine whether the coins specified by the user total the specified amount and give them feedback on their answer including whether they used the minimum or optimum number of coins to produce the amount. Sample output of a program...

  • Write a C program that calculates exact change. In order to receive full credit, please remember...

    Write a C program that calculates exact change. In order to receive full credit, please remember that only int arithmetic is exact, so you’ll need to break up your double into two ints, the one before the decimal point and the one after the decimal point. Another point worth mentioning is that the % operator gives the remainder. In other words, when working with int values, 9 / 5 = 1 whereas 9 % 5 = 4. Keep this in...

  • C++ HW Question Your program will simulate a simple change maker for a vending machine. It...

    C++ HW Question Your program will simulate a simple change maker for a vending machine. It will start with a stock of coins and dollars. It will then repeatedly request the price for an item to be purchased or to quit. If given a price, it will accept nickels, dimes, quarters, one-dollar and five-dollar bills—deposited one at a time—in payment. When the user has deposited enough to cover the cost of the item, the program will calculate the coins to...

  • Write a program with total change amount as an integer input, and output the change using...

    Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes

  • Describe how you would create a Python program that calculates the exact amount of change to...

    Describe how you would create a Python program that calculates the exact amount of change to give a customer in denominations of quarters, dimes, nickels, and pennies. For example, if the change is 79 cents, how many of each coin would be needed to give change?

  • I need to change this python program into one that counts back the LEAST AMOUNT OF...

    I need to change this python program into one that counts back the LEAST AMOUNT OF COINS. Ex: if i input 44 as my change, how can i make it show 4 dimes and 4 pennies instead of 1 quarter, a dime, and 9 pennies? heres my code so far in PYTHON " cents=int(input("Please enter the change in Cents: ")) Quarter= int(cents/25)#25cents=1 Quarter Dime =int (cents%25/10)# 10 cents=1 Dime cents =(cents%25)%10 print(Quarter,"quarters",Dime,"dimes and",cents,"cents") "

  • Write a program that tells what coins to give out for as change from 1 cent...

    Write a program that tells what coins to give out for as change from 1 cent to 99 cents. Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies) only. Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. Solution Demo Hello I am the coin machine! I will give you the least number of coins for your change....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT