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.

Practical Learning Notes for Calculate Compound Interest

This Python program is part of the "Basic Python Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.