0
0
PythonProgramBeginner · 2 min read

Python Program to Calculate Compound Interest

You can calculate compound interest in Python using the formula A = P * (1 + r/n) ** (n * t), where P is principal, r is annual rate (decimal), n is times compounded per year, and t is years; then compound interest is A - P.
📋

Examples

InputP=1000, r=0.05, n=4, t=1
OutputCompound Interest: 50.95
InputP=1500, r=0.043, n=2, t=6
OutputCompound Interest: 438.84
InputP=2000, r=0.06, n=1, t=0
OutputCompound Interest: 0.00
🧠

How to Think About It

To calculate compound interest, first understand that the total amount grows by adding interest on both the original principal and the interest already earned. Use the formula A = P * (1 + r/n) ** (n * t) to find the total amount after interest. Then subtract the principal P from this total to get the compound interest earned.
📐

Algorithm

1
Get the principal amount (P), annual interest rate (r), number of times interest is compounded per year (n), and time in years (t).
2
Calculate the total amount A using the formula A = P * (1 + r/n) raised to the power of (n * t).
3
Subtract the principal P from the total amount A to find the compound interest.
4
Return or print the compound interest rounded to two decimal places.
💻

Code

python
P = float(input('Enter principal amount: '))
r = float(input('Enter annual interest rate (in %): ')) / 100
n = int(input('Enter number of times interest compounded per year: '))
t = float(input('Enter time in years: '))

A = P * (1 + r / n) ** (n * t)
compound_interest = A - P
print(f'Compound Interest: {compound_interest:.2f}')
Output
Enter principal amount: 1000 Enter annual interest rate (in %): 5 Enter number of times interest compounded per year: 4 Enter time in years: 1 Compound Interest: 50.95
🔍

Dry Run

Let's trace the example with P=1000, r=5%, n=4, t=1 through the code

1

Input values

P=1000, r=5% converted to 0.05, n=4, t=1

2

Calculate total amount A

A = 1000 * (1 + 0.05/4) ** (4 * 1) = 1000 * (1 + 0.0125) ** 4 = 1000 * 1.050945 = 1050.945

3

Calculate compound interest

Compound Interest = 1050.945 - 1000 = 50.945

4

Print result

Compound Interest: 50.95 (rounded)

StepCalculationValue
Calculate A1000 * (1 + 0.05/4) ** 41050.945
Compound Interest1050.945 - 100050.945
💡

Why This Works

Step 1: Convert rate to decimal

The interest rate is given in percent, so dividing by 100 converts it to decimal form for calculation.

Step 2: Apply compound interest formula

The formula A = P * (1 + r/n) ** (n * t) calculates the total amount including interest compounded multiple times per year.

Step 3: Find compound interest

Subtracting the original principal P from the total amount A gives the compound interest earned.

🔄

Alternative Approaches

Using a function for reuse
python
def compound_interest(P, r, n, t):
    A = P * (1 + r / n) ** (n * t)
    return A - P

print(f'Compound Interest: {compound_interest(1000, 0.05, 4, 1):.2f}')
This approach makes the calculation reusable and cleaner for multiple inputs.
Using math.pow() function
python
import math
P = 1000
r = 0.05
n = 4
t = 1
A = P * math.pow((1 + r / n), n * t)
print(f'Compound Interest: {A - P:.2f}')
Using math.pow() is an alternative to the ** operator but both are equally efficient here.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation uses a fixed number of arithmetic operations and exponentiation, so it runs in constant time.

Space Complexity

Only a few variables are used to store inputs and results, so space usage is constant.

Which Approach is Fastest?

Both the direct formula and using math.pow() run in constant time; using a function adds clarity but no significant speed difference.

ApproachTimeSpaceBest For
Direct formula with ** operatorO(1)O(1)Simple scripts
Function-based approachO(1)O(1)Reusable code and multiple calculations
Using math.pow()O(1)O(1)When preferring math module functions
💡
Always convert the interest rate from percent to decimal before calculations by dividing by 100.
⚠️
Forgetting to divide the interest rate by 100, which leads to incorrect compound interest results.