Hello, help please, I am learning php and I can’t solve a fairly easy task

It's called "iPhone on credit" I take an iPhone on credit,

I pay everything seems normal, but at the end the bank owes me though so I shouldn’t have to pay the balance and that's it.

<?php error_reporting(-1); $creditBalance = 40000; /* Долг анона перед банком */ $percent = 1.03; /* Банк начисляет 3% в месяц от суммы */ $servicePayment = 1000; /* А также 1000 рублей в месяц комиссии за обслуживание счета */ $monthlyPayment = 5000; /* Анон платит 5000 р в месяц, это все, что ему дает мама на завтраки */ $paymentTotal = 0; /* Сколько всего отдал банку анон */ /* Посчитаем расходы 20 раз на 20 месяцев вперед */ for ($month = 1; $month <= 20; $month ++) { $creditBalance = ( $creditBalance * $percent ) + $servicePayment; // 4000 * 1.03 = if($creditBalance <= $monthlyPayment){ $creditBalance = $creditBalance - $monthlyPayment; $paymentTotal = $paymentTotal + $monthlyPayment; } else{ $creditBalance = $creditBalance - $monthlyPayment; $paymentTotal = $paymentTotal + $monthlyPayment; } echo "{$month} месяц спустя: долг = {$creditBalance} руб, выплачено всего {$paymentTotal} руб. \n"; if ($creditBalance <= 0){ echo "Ура я закончил\n\n"; break; } } 

here is the link http://ideone.com/m8yga9

  • one
    Look at the if($creditBalance <= $monthlyPayment) condition if($creditBalance <= $monthlyPayment) : you have the same branches. Maybe you need to do something else? - Im ieee
  • Tell me please, I all day long - Mikhail Volkov
  • Just guided by the “vital” logic: if the amount you need to pay (the $creditBalance variable) is less than the amount of money an anon has in stock ( $monthlyPayment variable), does anon need to give all the money to the bank? Or you can not all, but as much as you need? - Im ieee
  • @Imieee needs what would happen so Anon should pay for the 13 months exactly the balance and everything and now the bank owes the anon this is not correct, please help to write so I would pay the balance but the bank should not have left me - Mikhail Volkov

1 answer 1

 $credit = 40000; $persent = 1.03; $servicePayment = 1000; $monthlyPayment = 5000; $total = 0; $months = 0; while ( $credit >0 ){ $credit = ($credit * $persent) + $servicePayment; if ($monthlyPayment < $credit) { $credit -= $monthlyPayment; $total += $monthlyPayment; } else { # последняя выплата $total += $credit; $credit = 0; } $months++; printf ("Loan after $months months is %.02f RUR (%.02f RUR already payed)\n", $credit, $total); } 

Result:

 Loan after 1 mounths is 37200.00 RUR (5000.00 RUR already payed) Loan after 2 mounths is 34316.00 RUR (10000.00 RUR already payed) Loan after 3 mounths is 31345.48 RUR (15000.00 RUR already payed) Loan after 4 mounths is 28285.84 RUR (20000.00 RUR already payed) Loan after 5 mounths is 25134.42 RUR (25000.00 RUR already payed) Loan after 6 mounths is 21888.45 RUR (30000.00 RUR already payed) Loan after 7 mounths is 18545.11 RUR (35000.00 RUR already payed) Loan after 8 mounths is 15101.46 RUR (40000.00 RUR already payed) Loan after 9 mounths is 11554.50 RUR (45000.00 RUR already payed) Loan after 10 mounths is 7901.14 RUR (50000.00 RUR already payed) Loan after 11 mounths is 4138.17 RUR (55000.00 RUR already payed) Loan after 12 mounths is 262.32 RUR (60000.00 RUR already payed) Loan after 13 mounths is 0.00 RUR (61270.19 RUR already payed)