fork(1) download
  1. <?php
  2.  
  3.  
  4. function calculateLoan($loan, $percent, $monthlyPayment, $taxRate = 0)
  5. {
  6. $month = 0;
  7. $totalPayment = 0;
  8.  
  9. while ($loan > 0) {
  10. $totalPayment += $monthlyPayment;
  11. $loan += $loan * $percent;
  12.  
  13. if ($loan + $taxRate > $monthlyPayment) {
  14. $loan -= ($monthlyPayment - $taxRate);
  15. } else {
  16. $left = $monthlyPayment - ($loan + $taxRate);
  17. $loan = 0;
  18. }
  19.  
  20. $month++;
  21. }
  22.  
  23. return array(
  24. 'total' => round($totalPayment - $left, 2),
  25. 'month' => $month,
  26. 'left' => round($left, 2),
  27. );
  28. }
  29.  
  30. $loan = 40000;
  31. $monthlyPayment = 2000;
  32. $taxRate = $monthlyPayment * 0.03;
  33. $percent = 0.03;
  34.  
  35. $data = calculateLoan($loan, $percent, $monthlyPayment, $taxRate);
  36.  
  37. echo "Выплачено: " . $data['total'] . " рублей.\n" . "За " . $data['month'] . " месяцев.\n" . "В конце месяца на булочки осталось: " . $data['left'] . " рублей.\n";
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
Выплачено: 65242.4 рублей.
За 33 месяцев.
В конце месяца на булочки осталось: 757.6 рублей.