What's new

PHP PHP

Need Help ppl

Grasshopper
Philippine currency is broken down into 1000, 500, 100, 50, 20, 10, 5 and 1 peso coin. Write a PHP script that would accept an integer amount in peso and output the smallest bills and coins that would add up to that amount. Output the result using input boxes and marked then as read-only.

Eto yung code ko:
____________

<!DOCTYPE html>
<html>
<body>

<?php

function countCurrency($amount)
{
$notes = array(1000 , 500, 100, 50, 20, 10, 5, 1);
$noteCounter = array(0, 0, 0, 0, 0, 0, 0, 0, 0);

for ($i = 0; $i < 9; $i++)
{
if ($amount >= $notes[$i]){
$noteCounter[$i] = intval ($amount / $notes[$i]);
$amount = $amount - $noteCounter[$i] * $notes[$i];
}
}

echo ("Enter Amount: 3998 "."<br> \n");
for ($i = 0; $i < 9; $i++)
{
if ($noteCounter[$i] != 0){
echo ($notes[$i] . "bills : " . $noteCounter[$i] . "<br> \n");
}
}
}

$amount = 3998;
countCurrency($amount);

?>

</body>
</html>
 
Kasi yan yung output ng code mo, wala naman mali sa code mo, pero di yan yung sagot sa problem mo kung ang gusto mong mangyari ay lahat ng notes na less than sa input amount ay may value.

Kung icocompute mo manually malalaman mo kung bakit.
Code:
Amount: 3998

3998 / 1000 = 3
3998 - (3 * 1000) = 998

998 / 500 = 1
998 - (1 * 500) = 498

498 / 100 = 4
498 - (4 * 100) = 98

98 / 50 = 1
98 - (1 * 50) = 48

48 / 20 = 2
48 - (2 * 20) = 8

8 / 5 = 1
8 - (1 * 5) = 3

3 / 1 = 3
3 - (1 * 3) = 0
 
Last edited:
Kasi yan yung output ng code mo, wala naman mali sa code mo, pero di yan yung sagot sa problem mo kung ang gusto mong mangyari ay lahat ng notes na less than sa input amount ay may value.

Kung icocompute mo manually malalaman mo kung bakit.
Code:
Amount: 3998

3998 / 1000 = 3
3998 - (3 * 1000) = 998

998 / 500 = 1
998 - (1 * 500) = 498

498 / 100 = 4
498 - (4 * 100) = 98

98 / 50 = 1
98 - (1 * 50) = 48

48 / 20 = 2
48 - (2 * 20) = 8

8 / 5 = 1
8 - (1 * 5) = 3

3 / 1 = 3
3 - (1 * 3) = 0
ayun nasagot na ni idol haha
 

Similar threads

Back
Top