Bifalgorithm post · Computer science one · Paying the Piper

How does the PHP loan amortization program process monthly payments?

How does the PHP loan amortization program process monthly payments? (Computer science one · Paying the Piper) — "The full solution can be found in Code Sample…

Ideas in this flowchart

  • Code Sample 39.8 calculates and displays an amortized loan payment schedule.
  • The program checks whether exactly three command-line arguments were provided.
  • If the argument count is invalid, the program prints usage information and exits.
  • With valid arguments, the program converts the principal, APR, and number of months to numeric values.
  • The monthly interest rate is calculated by dividing the APR by twelve.
  • The monthly payment is calculated using the loan-payment formula.
  • The monthly payment is rounded to the nearest cent.
  • The program prints the principal, APR, number of months, and calculated monthly payment.
  • For the first n−1 payments, a loop processes each monthly installment.
  • Each iteration calculates the monthly interest from the current balance and rounds it.
  • The monthly principal payment is found by subtracting interest from the monthly payment.
  • The balance is reduced by the monthly principal payment.
  • Each iteration prints the payment number, interest, principal payment, and new balance.
  • The final month is handled separately.
  • The final interest is calculated from the remaining balance and rounded.
  • The last payment equals the remaining balance plus the final interest.
  • The program prints the amount of the last payment.
Feed/

How does the PHP loan amortization program process monthly payments?

No cover

Channel

Computer science one

1 page

"The full solution can be found in Code Sample 39.8.\n\n<?php\n\nif($argc != 4) {\n\nprintf(\"Usage: %s principle apr terms\n\n\", $argv[0]);\n\nexit(1);\n\n}\n\n$principle = floatval($argv[1]);\n\n$apr = floatval($argv[2]);\n\n$n = intval($argv[3]);\n\n$balance = $principle;\n\n$monthlyInterestRate = $apr / 12;\n\n//monthly payment\n\n$monthlyPayment = ($monthlyInterestRate * $principle) /\n\n(1 - pow( (1 + $monthlyInterestRate), -$n));\n\n//round to the nearest cent\n\n$monthlyPayment = round($monthlyPayment * 100) / 100;\n\nprintf(\"Principle: $%.2f\n\n\", $principle);\n\nprintf(\"APR: %.4f%%\n\n\", $apr * 100.0);\n\nprintf(\"Months: %d\n\n\", $n);\n\nprintf(\"Monthly Payment: $%.2f\n\n\", $monthlyPayment);\n\n//for the first n-1 payments in a loop:\n\nfor($i=1; $i<$n; $i++) {\n\n// compute the monthly interest, rounded:\n\n$monthlyInterest =\n\nround( ($balance * $monthlyInterestRate) * 100) / 100;\n\n// compute the monthly principle payment\n\n$monthlyPrinciplePayment = $monthlyPayment - $monthlyInterest;\n\n// update the balance\n\n$balance = $balance - $monthlyPrinciplePayment;\n\n// print i, monthly interest, monthly principle, new balance\n\nprintf(\"%d\\t$%10.2f $%10.2f $%10.2f\n\n\", $i, $monthlyInterest, $monthlyPrinciplePayment, $balance);\n\n}\n\n//handle the last month and last payment separately\n\n$lastInterest = round( ($balance * $monthlyInterestRate) * 100) / 100;\n\n$lastPayment = $balance + $lastInterest;\n\nprintf(\"Last payment = $%.2f\n\n\", $lastPayment);\n\n?>"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.