Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Compound Interest Calculator

Calculate compound interest with a chosen frequency. Students distinguish a per-period rate from an annual rate and multiply years by periods per year.

IntermediatePhase 6 - Category 5: Real-World Logic ProblemsExample 5 of 15
compound-interest-calculator.py
1# Take inputs
2principal = float(input("Enter principal: "))
3rate = float(input("Enter rate (%): "))
4time = float(input("Enter time (years): "))
5compounds = int(input("Enter compounds per year: "))
6
7# Calculate
8amount = principal * (1 + rate / (100 * compounds)) ** (compounds * time)
9interest = amount - principal
10
11print(f"Compound Interest: {interest:.2f}")
12print(f"Total Amount: {amount:.2f}")

Output

Enter principal: 10000
Enter rate (%): 5
Enter time (years): 2
Enter compounds per year: 12
Compound Interest: 1049.41
Total Amount: 11049.41

In this one

  • Convert annual to periodic rates
  • Count compounding periods
  • Separate interest from amount

How it works

  1. 1

    Read principal, annual rate, years, and frequency.

  2. 2

    Raise the periodic growth factor to frequency times years.

  3. 3

    Subtract principal and print interest and final balance.

Edge cases

What if compounds is zero?

What if compounds is zero?

The periodic-rate calculation raises ZeroDivisionError.

Is amount the same as interest?

Is amount the same as interest?

No. amount includes the original principal.

What's going on

The base factor is 1 + rate / (100 * compounds). Raising it to compounds * time applies that factor once per compounding period.

amount is the grown balance; subtracting principal gives interest alone. Twelve periods means monthly compounding under this formula, not twelve percent interest. A frequency of zero divides by zero, so frequency must be positive. The .2f display does not remove floating-point approximation.

What if compounds is zero? The periodic-rate calculation raises ZeroDivisionError.

Is amount the same as interest? No. amount includes the original principal.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the exponent?

Pick an answer to check it.

Question 2

What if compounds is zero?

Pick an answer to check it.

Questions people ask

What if compounds is zero?

The periodic-rate calculation raises ZeroDivisionError.

Is amount the same as interest?

No. amount includes the original principal.

What is the exponent?

compounds * time.