Drill

ProblemsPython › finance

Months to clear the loan

hardfinancePython

A loan charges whole-percent interest each month and takes a fixed monthly payment. Simulate month by month to see how long, if ever, it takes to clear.

loan_payoff(loan_minor: int, monthly_payment: int, monthly_rate_percent: int) → int

Solve it in the editor →

Where you start

def loan_payoff(loan_minor: int, monthly_payment: int, monthly_rate_percent: int) -> int:
    

Worked examples

CallResult
loan_payoff(100, 30, 0)4
loan_payoff(100, 100, 0)1
loan_payoff(50, 10, 0)5
loan_payoff(100, 50, 10)3

Hint

Walk the loop with a guard count; if you run out of iterations, the debt never dies.

Reference solution in Python
def loan_payoff(loan_minor: int, monthly_payment: int, monthly_rate_percent: int) -> int:
    balance, months = loan_minor, 0
    while balance > 0 and months < 1200:
        balance += balance * monthly_rate_percent // 100
        balance -= monthly_payment
        months += 1
    return -1 if balance > 0 else months

The same problem in another language

More finance problems in Python