<?php
fscanf(STDIN, "%f", $value);
$cem = 0;
$cinquenta = 0;
$vinte = 0;
$dez = 0;
$cinco = 0;
$dois = 0;
$UmReal = 0;
$CinquentaCentavos = 0;
$VintEeCincoCentavos = 0;
$DezCentavos = 0;
$CincoCentavos = 0;
$UmCentavo = 0;
// -- transformar tudo em centavos (*100)
// -- para que a subtração em CENTAVOS não gere loop infinito
// -- com float, $value na subtração não iria chegar em 0
// -- $value ia se tornar um decimal cada vez menor
// -- ex: 0.001, 0.0001, 0.00001, e como não há if's para $value menor que 0.01
// -- o do while nunca pararia
// -- por isso transformar em inteiro (*100), 0.01*100 = 1, $value -= 1 -> = (int) $value
$rest = $value*100;
do {
if ($rest >= 10000) {
$cem += 1;
$rest -= 10000;
continue;
}
if ($rest >= 5000) {
$cinquenta += 1;
$rest -= 5000;
continue;
}
if ($rest >= 2000) {
$vinte += 1;
$rest -= 2000;
continue;
}
if ($rest >= 1000) {
$dez += 1;
$rest -= 1000;
continue;
}
if ($rest >= 500) {
$cinco += 1;
$rest -= 500;
continue;
}
if ($rest >= 200) {
$dois += 1;
$rest -= 200;
continue;
}
if ($rest >= 100 && $rest < 200) {
$UmReal += 1;
$rest -= 100;
continue;
}
// -- CENTAVOS --
if ($rest >= 50) {
$CinquentaCentavos += 1;
$rest -= 50;
continue;
}
if ($rest >= 25) {
$VintEeCincoCentavos += 1;
$rest -= 25;
continue;
}
if ($rest >= 10) {
$DezCentavos += 1;
$rest -= 10;
continue;
}
if ($rest >= 5) {
$CincoCentavos += 1;
$rest -= 5;
continue;
}
if ($rest >= 1) {
$UmCentavo += 1;
$rest -= 1;
continue;
}
} while ($rest > 0);
printf("NOTAS:\n%d nota(s) de R$ 100.00\n%d nota(s) de R$ 50.00\n%d nota(s) de R$ 20.00\n%d nota(s) de R$ 10.00\n%d nota(s) de R$ 5.00\n%d nota(s) de R$ 2.00\nMOEDAS:\n%d moeda(s) de R$ 1.00\n%d moeda(s) de R$ 0.50\n%d moeda(s) de R$ 0.25\n%d moeda(s) de R$ 0.10\n%d moeda(s) de R$ 0.05\n%d moeda(s) de R$ 0.01\n", $cem,$cinquenta,$vinte,$dez,$cinco,$dois,$UmReal,$CinquentaCentavos,$VintEeCincoCentavos,$DezCentavos,$CincoCentavos,$UmCentavo);
?>
To embed this project on your website, copy the following code and paste it into your website's HTML: