A borrower pays the same amount every month. Read three lines: the amount borrowed, the
yearly interest rate in percent and the monthly payment. Amounts have 0-2 decimals and
may use comma thousands separators (12,500.5); the rate has 0-3 decimals.
Each month, in this order:
- interest = balance × rate / 100 / 12, rounded to the nearest cent, halves up; add it to the balance;
- if the balance is now at most the payment, the borrower pays exactly the balance and the loan is finished; otherwise the payment is subtracted and the next month starts.
If the loan finishes, print:
Payments: <number of payments>
Last payment: <money>
Total interest: <money>
Duration: <duration>
Money is $ + dollars with comma separators + . + two-digit cents. The duration uses years of
12 months: <Y> year(s) <M> month(s), with year/month in the singular for 1, and a part
left out when it is 0 (1 year, 2 years 1 month, 7 months).
If the loan can never finish (the interest is at least the payment while the balance is still
larger than the payment), print only Never paid off.
Examples
Input:
1,000
12
300
Output:
Payments: 4
Last payment: $122.48
Total interest: $22.48
Duration: 4 months
Input:
10000
6
50
Output:
Never paid off
Constraints
- borrowed amount between
0.01and9,999,999.99; rate between0and40 - payment at least
0.01; a loan that finishes needs at most3000payments
Goals
- Simulate month-by-month interest with rounding to the cent at every step
- Detect a payment that can never finish the loan
- Print durations with correct singular and plural words