fork download
  1. <?php
  2.  
  3.  
  4. $creditBalance = 1000;
  5. $percent = 1.03;
  6. $serviceFee = 1000;
  7. $monthlyPayment = 5000;
  8. $paymentTotal = 0;
  9.  
  10. for ($months = 1; $months <= 20; $months ++) {
  11. $creditBalance = ($creditBalance * $percent) + $serviceFee - $monthlyPayment;
  12. $paymentTotal = $paymentTotal + $monthlyPayment;
  13. echo "At the end of the month #{$months}: debt = {$creditBalance}, total payment by now = {$paymentTotal} \n";
  14.  
  15. if (($creditBalance * $percent) + $serviceFee < 5000) {
  16.  
  17. $paymentTotal = $paymentTotal + ($creditBalance * $percent) + $serviceFee;
  18. $months++;
  19. break;
  20. }
  21. }
  22.  
  23. echo "Eventually in the month #{$months} the boy has totally paid {$paymentTotal}";
  24.  
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
At the end of the month #1: debt = -2970, total payment by now = 5000 
Eventually in the month #2 the boy has totally paid 2940.9