KatInterview

Simple Interest Earned on a Short-Term Loan

Math & Bit ManipulationAuto-graded · 18 test cases
EasyAuto-gradedMath & Bit Manipulationmathsimple-interestarithmetic

Last updated

Compute the interest earned on a loan using a simple (non-compounding) annual percentage rate prorated over a number of months.

A lender issues a single-payment loan. The borrower receives a principal amount, and at the end of the term repays the principal plus interest in one lump sum.

Interest is charged as simple interest at a given annual percentage rate (APR). Assume the following:

  • There is no compounding.
  • There is no discounting for inflation or time value of money.
  • The lender has no cost of funds, no fees, and no defaults.
  • The borrower always repays in full.

Under these assumptions, the lender's profit equals the total interest collected. Write a function that returns this profit.

Input

  • principal: the amount lent, as a number (the loan amount in dollars).
  • apr: the annual interest rate expressed as a percentage, so 30 means 30% per year (not 0.30).
  • months: the length of the loan in whole months.

Output

Return the profit (the total interest earned) as a number. It is the principal multiplied by the annual rate as a fraction, multiplied by the loan term expressed as a fraction of a year (months / 12). Do not include the principal itself in the result, and do not round the result.

Constraints

  • principal is a non-negative number.
  • apr is a non-negative number given in percent units.
  • months is a non-negative integer.
  • The loan is never compounded, and the term is prorated linearly (each month is exactly 1/12 of a year).

Examples

Example 1

Input: simple_loan_interest(1000, 30, 4)

Output: 100

Example 2

Input: simple_loan_interest(5000, 12, 12)

Output: 600

Example 3

Input: simple_loan_interest(250, 24, 3)

Output: 15

Hints

Hint 1

Convert the percentage APR into a fraction by dividing by 100.

Hint 2

A loan lasting `months` months covers `months / 12` of a year, and with no compounding the interest scales linearly with time.

Hint 3

Profit is just the interest: principal times the annual rate fraction times the year fraction. Do not add the principal back.