PHP - Pennies in a Dollar

TimG

Gawd
Joined
Jan 30, 2006
Messages
919
I've got this homework project I'm struggling with and I'm looking for a little direction.

I'm supposed to write a script that asks the user how many pennies they have, and then it outputs the number of dollars, quarters, dimes, nickles, and pennies that is.

What would be the best way to go about doing this? I'm at a complete loss.
 
Do you have any idea about the steps you have to take to solve this problem? Just saying "I have no idea what I'm doing" won't get you far.

How much PHP do you know? What have you learned, if anything? Have you tried sitting down and writing down ideas on paper/whiteboard/etc?

If you really are at a complete loss, read this tutorial:

http://www.tizag.com/phpT/index.php

and you will be especially interested in form processing:

http://www.tizag.com/phpT/forms.php
 
The part I don't get is how to get the left over.

Say the user has 106 pennies.

That would be 1 dollar, 1 nickel, and 1 penny.

I know all about forms and $_POST. This is what I have right now, which just displays the number of coins that makes up the amount the user entered, which really isn't what my teacher is asking for.

Code:
<?php
$totalAmount = $_POST['pennies'];
$dime = 10;
$nickel = 05;
$penny = 01;
$quarter = 25;
$dollar = 100;

$quarterAmount = $totalAmount / $quarter;
$dimeAmount = $totalAmount / $dime;
$nickelAmount = $totalAmount / $nickel;
$pennyAmount = $totalAmount / $penny;
$dollarAmount = $totalAmount / $dollar;
$totalAmount = $quarterAmount + $dimeAmount;
// Prints # of Quarters
echo floor($quarterAmount) .' quarters';
echo '<br />';
// Prints % of Dimes
echo floor($dimeAmount) . ' dimes';
echo '<br />';
// Prints # of Nickels
echo floor($nickelAmount) .' nickels';
echo '<br />';
// Prints # of Pennies
echo floor($pennyAmount) .' pennies' ;
echo '<br />';
// Prints # of Dollars
echo floor($dollarAmount) .' dollars';
?>
 
So I'm a little confused - if the user says they have 543 pennies, for example, does your teacher want output like this?

Code:
543 pennies is...

5 dollars
1 quarter
1 dime
1 nickel
3 pennies

as compared to yours which would produce something like this, due to rounding down:

Code:
543 pennies is...

5 dollars
21 quarters
54 dimes
108 nickels
543 pennies
 
eh, don't know php well enough but the logic is fairly simple

$quarterAmount = floor($totalAmount / $quarter);
$totalAmount = $totalAmount - ($quarterAmount * $quarter).

$dimeAmount = $totalAmount / $dime;
$totalAmount = $totalAmount - ($dimeAmount * $dime).

etc
 
So I'm a little confused - if the user says they have 543 pennies, for example, does your teacher want output like this?

Code:
543 pennies is...

5 dollars
1 quarter
1 dime
1 nickel
3 pennies

Yes, that.

eh, don't know php well enough but the logic is fairly simple

$quarterAmount = floor($totalAmount / $quarter);
$totalAmount = $totalAmount - ($quarterAmount * $quarter).

$dimeAmount = $totalAmount / $dime;
$totalAmount = $totalAmount - ($dimeAmount * $dime).

etc

Hmm, interesting. Wouldn't $totalAmount get overwritten each time and just display whatever is written last?
 
%

this is a trivially simple math problem.

get rid of that / and floor nonsense and look at the % operator.
 
This is not really a php thing, it's more or less a math and logic thing. Maybe a couple loops and what not. I think I remember having to do this in college. The % operator comes in handy. (note, it is NOT percentage, google what it's for in programming)
 
In that case it's more of changing your math around, as jiminator started doing.

Do you know the modulus operator?

http://php.net/manual/en/language.operators.arithmetic.php

Basically, you want to take your amount and start reducing it by dollars...quarters...etc.

I'm not a PHP programmer, but its syntax is C-like so you should be able to follow along.

Code:
int pennies = Some Amount Of Pennies

// you probably want to reduce the pennies by the amount of the largest currency denomination first, so in your instance, you want dollars first.
// After you get the # of dollars possible with those pennies, you want to reduce the number of pennies you have by the amount of pennies you used for dollars (hint: modulus)
// repeat down the currency chain

That should get you where you need to go
 
Where are you getting 543? That just a random number you thought of?

I definitely remember talking about modulus during JS class.

This was definitely what I needed though guys, thanks a lot. Just a little push in the right direction. Thanks.

EDIT - NM, you edited your post.
 
Where are you getting 543? That just a random number you thought of?

I definitely remember talking about modulus during JS class.

This was definitely what I needed though guys, thanks a lot. Just a little push in the right direction. Thanks.

Sorry, just an example. Replace 543 with whatever your user gave you on the form.
 
Could you re-post what you had in the code box before you edited it? I went back to look at it and it changed. The only code that's there is "int pennies = some amount of pennies.
 
The issue you're having is not with programming, but with developing an algorithm which will work.

What mathematical operations do you need to apply to the number of pennies to get the number of dollars, quarters, etc? You're obviously able to do that, since you can make change, so write down explicitly what that procedure is.

Once you've done that the programming will come easily.
 
Last edited:
learn something...

PHP:
$denominations = array(
	'dollars' => 100,
	'quarters' => 25,
	'dimes' => 10,
	'nickels' => 5,
	'pennies' => 1
);

$pennies = intval($_POST['pennies']);

foreach ($denomications as $name => $amount) {
	echo floor($pennies / $amount) . ' ' . $name . '<br />';
	$pennies -= ($pennies % $amount);
}
 
learn something...

PHP:
$denominations = array(
	'dollars' => 100,
	'quarters' => 25,
	'dimes' => 10,
	'nickels' => 5,
	'pennies' => 1
);

$pennies = intval($_POST['pennies']);

foreach ($denomications as $name => $amount) {
	echo floor($pennies / $amount) . ' ' . $name . '<br />';
	$pennies -= ($pennies % $amount);
}

There is an error in this algorithm. The "-=" should just be "=".
 
Back
Top