Python Program for compound interest

In this article, we will learn how to calculate compound interest using Python. Compound interest is the interest calculated on both the initial principal and the accumulated interest from previous periods.

Problem statement ? We are given three input values: principal amount, interest rate, and time period. We need to compute the compound interest.

Formula

The formula for calculating compound interest is:

Compound Interest = P × (1 + R/100)^T

Where:

  • P is the principal amount (initial investment)
  • R is the annual interest rate (in percentage)
  • T is the time period (in years)

Basic Implementation

Here's a simple function to calculate compound interest ?

def compound_interest(principal, rate, time):
    CI = principal * (pow((1 + rate / 100), time))
    print("Compound interest:", CI)

# Calculate compound interest
compound_interest(10000, 7.78, 2)
Compound interest: 11616.528400000001

Enhanced Implementation

Let's create a more comprehensive version that shows both compound interest and final amount ?

def calculate_compound_interest(principal, rate, time):
    # Calculate final amount
    amount = principal * (pow((1 + rate / 100), time))
    
    # Calculate compound interest
    compound_interest = amount - principal
    
    return amount, compound_interest

# Example calculation
principal = 15000
rate = 10.5
time = 3

amount, ci = calculate_compound_interest(principal, rate, time)

print(f"Principal Amount: ${principal}")
print(f"Interest Rate: {rate}%")
print(f"Time Period: {time} years")
print(f"Final Amount: ${amount:.2f}")
print(f"Compound Interest: ${ci:.2f}")
Principal Amount: $15000
Interest Rate: 10.5%
Time Period: 3 years
Final Amount: $20225.33
Compound Interest: $5225.33

Interactive Calculator

Here's a user-friendly calculator that takes input and handles different scenarios ?

def compound_interest_calculator():
    print("=== Compound Interest Calculator ===")
    
    # Sample inputs (in real scenario, you'd use input())
    principal = 25000
    rate = 8.5
    time = 5
    
    print(f"Principal: ${principal}")
    print(f"Rate: {rate}% per annum")
    print(f"Time: {time} years")
    
    # Calculate compound interest
    final_amount = principal * ((1 + rate/100) ** time)
    compound_interest = final_amount - principal
    
    print(f"\nResults:")
    print(f"Final Amount: ${final_amount:.2f}")
    print(f"Compound Interest Earned: ${compound_interest:.2f}")
    print(f"Interest Multiplier: {final_amount/principal:.2f}x")

# Run the calculator
compound_interest_calculator()
=== Compound Interest Calculator ===
Principal: $25000
Rate: 8.5% per annum
Time: 5 years

Results:
Final Amount: $37727.89
Compound Interest Earned: $12727.89
Interest Multiplier: 1.51x

Comparison with Simple Interest

Let's compare compound interest with simple interest to see the difference ?

def compare_interests(principal, rate, time):
    # Simple Interest
    simple_interest = (principal * rate * time) / 100
    
    # Compound Interest
    compound_amount = principal * ((1 + rate/100) ** time)
    compound_interest = compound_amount - principal
    
    print(f"Principal: ${principal}")
    print(f"Rate: {rate}% per annum")
    print(f"Time: {time} years")
    print(f"\nSimple Interest: ${simple_interest:.2f}")
    print(f"Compound Interest: ${compound_interest:.2f}")
    print(f"Difference: ${compound_interest - simple_interest:.2f}")

# Compare both methods
compare_interests(20000, 12, 4)
Principal: $20000
Rate: 12% per annum
Time: 4 years

Simple Interest: $9600.00
Compound Interest: $11493.74
Difference: $1893.74

Conclusion

Compound interest calculation in Python is straightforward using the formula P × (1 + R/100)^T. The pow() function or ** operator can be used for exponentiation. Compound interest grows exponentially compared to simple interest, making it powerful for long-term investments.

Updated on: 2026-03-25T06:27:23+05:30

626 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements