What's new

PHP [HELP] Currency Denomination

rakhistha

Eternal Poster
Established
Joined
Nov 2, 2014
Posts
1,627
Reaction
295
Points
426
Age
36
Pahelp naman po sa code. Sa image po ang target kong output. Kaso pag run ko ng code, ung mga need lang na denomination ang nagaappear.

1619436074492.png


<?php
function countCurrency($amount)
{
$notes = array(1000, 500, 100, 50, 20, 10, 5, 1, 0.50, 0.25, 0.10, 0.05, 0.01);
$noteCounter = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

// count notes using greedy approach
for ($i = 0; $i < 13; $i++)
{
if ($amount >= $notes[$i])
{
$noteCounter[$i] = intval($amount / $notes[$i]);
$amount = $amount - $noteCounter[$i] * $notes[$i];
}
}
// Print notes
echo nl2br("Currency Denomination"."\n"."\n");
for ($i = 0; $i < 13; $i++)
{
if ($noteCounter[$i] != 0)
{
echo nl2br($notes[$i] . " peso Bills : " . $noteCounter[$i] . "\n");
}

}
}
// Driver Code
$amount = 1375.29;
countCurrency($amount);
?>

pahelp po sa code..salamat..
 

Attachments

Trace muna lang ts,
may binago na ako dyan.

Tanong ka lang kung may question ka :ROFLMAO::ROFLMAO::ROFLMAO:

<?php
function type($amount) {
if ($amount >= 20) {
return "peso Bills";
} else if ($amount < 20 && $amount >= 1) {
return "peso Coins";
} else {
return "cents";
}
}

function countCurrency($amount) {
$notes = [1000, 500, 100, 50, 20, 10, 5, 1, 0.50, 0.25, 0.10, 0.05, 0.01];
$noteCounter = [];
$totalNotes = count($notes);
// count notes using greedy approach
for ($i = 0; $i < $totalNotes; $i++) {
$nc = intval(round($amount / $notes[$i], 2));
array_push($noteCounter, $nc);
$amount = $amount - $nc * $notes[$i];
}
// Print notes
echo "Currency Denomination" . "<br><br>";
for ($i = 0; $i < $totalNotes; $i++) {
echo $notes[$i] . " " . type($notes[$i]) . " : " . $noteCounter[$i] . "<br>";
}
}
// Driver Code
$amount = 1375.29;
countCurrency($amount);
?>
 
Last edited:
HH_PRINCE meron bang mali..bakit kaya hindi ko mainsert ung isang code..ok naman ung syntax..kaso sa variable declaration tama naman..

1619441011675.png


<?php
function countCurrency($amount) {
$notes = [1000, 500, 100, 50, 20, 10, 5, 1, 0.50, 0.25, 0.10, 0.05, 0.01];
$noteCounter = [];
$totalNotes = count($notes);
// count notes using greedy approach
for ($i = 0; $i < $totalNotes; $i++) {
$nc = intval(round($amount / $notes[$i], 2));
array_push($noteCounter, $nc);
$amount = $amount - $nc * $notes[$i];
}
// Print notes
echo nl2br('<h3>Amount in Pesos:'.$amount."\n"."\n"); //eto ang dinagdag ko..kaso naging ganun ung amount.
echo "Currency Denomination" . "<br><br>";
for ($i = 0; $i < $totalNotes; $i++) {
echo $notes[$i] . " " . type($notes[$i]) . " : " . $noteCounter[$i] . "<br>";
}
}
// Driver Code
$amount = 1375.29; //pwed po edit nalang dito ung amount.
countCurrency($amount);
function type($amount) {
if ($amount >= 20) {
return "peso Bills";
} else if ($amount < 20 && $amount >= 1) {
return "peso Coins";
} else {
return "cents";
}
}
?>
 

Attachments

mag-declare ka ng variable to store
yung original amount kasi modified na ang amount na variable.

mas mabuting mag "<br>" kana lang rin ts para wala ka nang tatawagin
na function echo lang naman yan.
 
PHP:
<?php

$total = 2644.67;

function cointype($n){
  if($n >= 20) return "bills";
  if($n >= 1 && $n <= 10) return "coins";
  if($n >= 0.1 && $n <= 0.5) return "cents";
  if($n == 0.05) return "centavos";
  if($n == 0.01) return "centavo";
}

function get($n){
  global $total;
  $type = cointype($n);
  if($total > $n){
    echo "${n} ${type}: ". floor($total / $n)."<br />";
    $total = fmod($total, $n);
  }else{
    echo "${n} ${type}: 0<br />";
  }
}

$denom = array(1000, 500, 100, 50, 20, 10, 5, 1, 0.50, 0.25, 0.10, 0.05, 0.01);

foreach($denom as $denoms){
  get($denoms);
}

try lang 😂

ito nagawa ko 🤣 pang exercise lang

bale yung idea ay divide and get the whole number

then get yung remainder and set
 
Last edited:
Graduate ako ng 2008 batch..pero shaks..etong coding tlga nahihirapan ako..hai..not a programmer tlga..im more on the cisco part..
 

Similar threads

Back
Top