Calculate Compound Interest

Calculate compound interest given principal, rate, time, and number of times interest is compounded per year.

BeginnerTopic: Basic Python Programs
Back

What You'll Learn

  • Using exponentiation with **
  • Implementing the compound interest formula
  • Working with multiple numeric inputs

Python Calculate Compound Interest Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to calculate compound interest

principal = float(input("Enter principal amount: "))
rate = float(input("Enter annual interest rate (in %): "))
time = float(input("Enter time in years: "))
n = int(input("Enter number of times interest applied per year: "))

amount = principal * (1 + rate / (100 * n)) ** (n * time)
compound_interest = amount - principal

print("Compound Interest is:", compound_interest)
print("Total Amount is:", amount)
Output
Enter principal amount: 1000
Enter annual interest rate (in %): 5
Enter time in years: 2
Enter number of times interest applied per year: 4
Compound Interest is: 104.486... 
Total Amount is: 1104.486...

Step-by-Step Breakdown

  1. 1Read principal, rate, time, and compounding frequency from the user.
  2. 2Compute the growth factor (1 + rate / (100 * n)).
  3. 3Raise the factor to the power n * time.
  4. 4Multiply by principal to get total amount.
  5. 5Subtract principal to get compound interest.

Understanding Calculate Compound Interest

Compound interest uses the formula:

\[ A = P \left(1 + \frac{R}{100n}\right)^{nT} \]

Where:

P is principal,
R is annual rate in percent,
T is time in years,
n is the number of times interest is compounded per year,
A is the final amount.

We then compute compound interest as A - P. This showcases exponentiation with ** in Python.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.