AlgoMaster Logo

Simple Interest and Compound Interest

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

Two ways money grows when it earns interest. Both start from the same principal, rate, and number of years, but they treat the interest already earned differently.

Simple interest ignores the interest already earned. Every year you earn the same fixed amount, calculated only on the original principal. Start with 1000 at 5 percent and you earn 50 every year, no matter how many years pass.

Compound interest reinvests the interest. At the end of each year, the interest earned is added to the balance, and the next year earns interest on that larger balance. The interest itself earns interest, which is why the formula has an exponent: the balance is multiplied by (1 + rate/100) once per year.

Compute both values from the same inputs and return them together, each rounded to two decimal places.

Key Constraints:

  • principal > 0 → There is always a real starting amount, so you never have to handle a zero or negative principal.
  • rate >= 0 → The rate can be zero. When it is, both interests come out to zero, since multiplying by (1 + 0/100) leaves the balance unchanged.
  • time >= 0 (integer years) → Time is a whole number of years. When it is zero, no interest accrues and both values are zero. Because time is an integer, you can compute the compound factor with a plain loop instead of a power function.

Approach 1: Apply the Interest Formulas

Intuition

Simple interest is one flat expression: principal * rate * time / 100, with no loop and no exponent.

Compound interest needs the factor (1 + rate/100)^time. Since time is an integer, you do not need a power function. Start a running product at 1 and multiply by (1 + rate/100) once per year. This keeps the code free of library calls and makes the compounding visible, since each multiplication is one year passing. The compounded balance is principal times that factor, and the interest alone is that balance minus the original principal.

Algorithm

  1. Compute the simple interest directly as principal * rate * time / 100.
  2. Build the compound factor. Start a variable factor at 1. Loop time times, multiplying factor by (1 + rate/100) on each pass.
  3. The compounded balance is principal * factor. Subtract principal to get the compound interest alone.
  4. Round both the simple interest and the compound interest to two decimal places.
  5. Return them as a two-element array [simpleInterest, compoundInterest].

Example Walkthrough

Take principal = 1000, rate = 5, time = 2.

1000
principal

Simple interest is a single calculation: 1000 * 5 * 2 / 100 = 100. Each year earns 50, and over two years that is 100.

For compound interest, start with factor = 1 and multiply by (1 + 5/100) = 1.05 twice, once for each year:

  • After year 1: factor = 1.05, balance = 1000 * 1.05 = 1050.
  • After year 2: factor = 1.05 * 1.05 = 1.1025, balance = 1000 * 1.1025 = 1102.5.

The compounded balance is 1102.5, so the compound interest is 1102.5 - 1000 = 102.5. Notice it beats the simple interest by 2.5, which is the interest earned on year one's 50 during year two.

Rounded to two decimals, the answer is:

0
100
1
102.5
result

Code