Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
2
Indexable
Never
<?php 
    $sisa = 0;
    $jenis = "";
    $total_jenis = [
        "keping" => 0,
        "lembar" => 0
    ];

    $nominal = [
        "100000" => 0, "50000" => 0, "20000" => 0, "10000" => 0, "5000" => 0, 
        "2000" => 0, "1000" => 0, "500" => 0, "200" => 0, "100" => 0
    ];

    $key = array_keys($nominal); 
    $input = (int) readline('Masukkan jumlah uang : ');
    echo "--------------------------------------\n";
    for ($i = 0; $i < count($nominal); $i++){
        while ($input >= $key[$i]){
            $nominal[$key[$i]] += 1;
            $input -= $key[$i];

            if ($i <= 6){
                $total_jenis["lembar"] += 1;
            } else {
                $total_jenis["keping"] += 1;
            }
        }

        if ($i <= 6){
            $jenis = "Lembar";
        } else {
            $jenis = "Keping";
        }
        

        if ($i == 0) {
            echo "Pecahan Rp. " . str_pad($key[$i], 7," ", STR_PAD_LEFT) . " = " . str_pad($nominal[$key[$i]], 3," ", STR_PAD_LEFT) . " $jenis\n";
        } else {
            echo "        Rp. " . str_pad($key[$i], 7," ", STR_PAD_LEFT) . " = " . str_pad($nominal[$key[$i]], 3," ", STR_PAD_LEFT). " $jenis\n";
        }
    }
    echo "Sisa uang tanpa pecahan : Rp. $input\n";
    echo "--------------------------------------\n";
    echo "Total jumlah lembaran\t= " . str_pad($total_jenis["lembar"], 3," ", STR_PAD_LEFT)  . str_pad(" Lembar", 3," ", STR_PAD_RIGHT) . "\n";
    echo "Total jumlah koin\t= " . str_pad($total_jenis["keping"], 3," ", STR_PAD_LEFT) . str_pad(" Keping", 3," ", STR_PAD_RIGHT) . "\n";
?>