beginnerLogical ThinkingJava

Simple and Compound Interest Calculator

Given principal, annual rate, and time in years, compute both the simple and compound interest.

Quick Answer

Simple interest is P * R * T / 100. Compound interest (compounded annually) is P * (1 + R/100)^T - P, where P is the principal, R the annual rate percentage, and T the time in years. Compute the power with Math.pow, then subtract the principal to get just the interest.

Problem Statement

Given a principal amount P, an annual interest rate R (as a percentage), and a time T in years, compute two values: the simple interest and the compound interest (compounded once per year).

Use the standard formulas: simple interest is P * R * T / 100, and compound interest is P * (1 + R/100)^T - P. For example, with P = 1000, R = 10, T = 2, the simple interest is 200.0 and the compound interest is 210.0.

Input: Three numbers: principal P, annual rate R (percent), and time T in years.

Output: Two numbers: the simple interest and the compound interest.

Examples

Example 1
Input:  P=1000, R=10, T=2
Output: SI=200.0, CI=210.0

SI = 1000*10*2/100 = 200; CI = 1000*(1.1)^2 - 1000 = 1210 - 1000 = 210.

Example 2
Input:  P=5000, R=5, T=3
Output: SI=750.0, CI=788.13

SI = 5000*5*3/100 = 750; CI = 5000*(1.05)^3 - 5000 = 5788.125 - 5000 = 788.125 (~788.13).

Constraints

  • P >= 0, R >= 0, T >= 0
  • Compounding is annual (once per year)

Think Before You Code

Reveal the questions to ask yourself first
  • How does the compound formula differ from simple interest as time grows?
  • Why do you subtract the principal after applying the compound growth factor?
  • Which parts of the calculation must be floating point to stay accurate?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Simple interest is a direct product: `P * R * T / 100`.
Hint 2
For compound interest, first find the growth factor `(1 + R/100)` and raise it to the power `T` with `Math.pow`.
Hint 3
The compound *interest* is the final amount minus the original principal, so subtract `P` at the end.

Approach

Reveal the step-by-step approach

Translate the two finance formulas straight into code.

  1. Compute simple interest: si = P * R * T / 100.
  2. Compute the compound amount: amount = P * Math.pow(1 + R / 100, T).
  3. Compound interest is that amount minus the principal: ci = amount - P.
  4. Return or print both si and ci.

Keep P, R, and T as double so the division by 100 and the exponentiation do not lose precision to integer truncation.

Dry Run

Walk through the example step by step

Interest for P = 1000, R = 10, T = 2:

Simple:
  si = P * R * T / 100 = 1000 * 10 * 2 / 100 = 20000 / 100 = 200.0

Compound:
  factor = 1 + R/100 = 1 + 0.10 = 1.10
  amount = P * factor^T = 1000 * 1.10^2 = 1000 * 1.21 = 1210.0
  ci     = amount - P   = 1210.0 - 1000 = 210.0

Solution

Reveal the full Java solution
public class InterestCalculator {
    public static double simpleInterest(double p, double r, double t) {
        return p * r * t / 100.0;
    }

    public static double compoundInterest(double p, double r, double t) {
        double amount = p * Math.pow(1 + r / 100.0, t);
        return amount - p;
    }

    public static void main(String[] args) {
        System.out.println(simpleInterest(1000, 10, 2));   // 200.0
        System.out.println(compoundInterest(1000, 10, 2)); // 210.0 (approx)
        System.out.println(simpleInterest(5000, 5, 3));    // 750.0
        System.out.println(compoundInterest(5000, 5, 3));  // 788.125 (approx)
    }
}

Simple interest grows linearly with time, so it is a plain multiplication. Compound interest grows by a fixed factor each period; raising (1 + R/100) to the power T gives the total amount, and subtracting the principal isolates the interest earned.

Dividing by 100.0 (a double) rather than 100 avoids integer division, and Math.pow handles the yearly compounding. Note that binary floating point can show tiny tails like 788.1250000000005; format to two decimals for display when presenting currency.

Time: O(1)Space: O(1)

Common Mistakes

  • Dividing by an integer 100, truncating R/100 to 0 for small rates.
  • Reporting the compound *amount* instead of the interest by forgetting to subtract the principal.

Edge Cases to Test

  • T = 0 gives both simple and compound interest of 0.
  • R = 0 gives 0 interest for any principal and time.
  • Floating-point output may need rounding to two decimals for money values.

Interview Follow-Ups

  • How would you support compounding n times per year instead of annually?
  • How would you round the results to exactly two decimal places for a receipt?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us