Given a principal amount P, an annual interest rate R (in percent), a time period T (in years) and a compounding frequency n (times per year), write a Python program that calculates the compound interest and the total amount after T years.
- Input: Principal (P), Rate (R in percent), Time (T in years), Compounding frequency (n)
- Output: Compound interest (CI) and Amount (A)
Keep a space where the program output will appear so you can copy-paste the program output after you run it.
Approach / Algorithm
- Convert the annual rate from percent to decimal:
r = R / 100.0. - If compounding frequency
nis provided, computeA = P * (1 + r/n)**(n*t). - Otherwise (if continuous compounding), compute
A = P * math.exp(r*t). - Compound interest
CI = A - P. - Print the results with sensible formatting.
Method 1 – Python program to calculate the compound interest
# Method 1: Simple, beginner-friendly version (no function)
P = float(input("Enter principal amount (P): "))
R = float(input("Enter annual interest rate (R) in %: "))
T = float(input("Enter time (T) in years: "))
n = int(input("Enter compounding frequency per year (n) [e.g., 1,4,12]: "))
# convert percent to decimal
r = R / 100.0
# compute amount and compound interest using the direct formula
A = P * (1 + r / n) ** (n * T)
CI = A - P
print(f'''
Amount after {T} years: {A:.4f}")
print(f"Compound interest: {CI:.4f}''')
Output:

Explaination : This simple version is written without functions so beginners can follow each step easily. It reads inputs, converts the rate from percent to decimal, applies the compound-interest formula directly, then prints the final amount and interest. Use this when you want a short, runnable example that teaches the formula and the order of operations.
Method 2 – Python program to calculate the compound interest
# Method 2: Simulate compounding period-by-period
def compound_interest_iterative(principal, annual_rate_percent, years, compounding_per_year=1):
r = annual_rate_percent / 100.0
periods = int(compounding_per_year * years)
amount = principal
for _ in range(periods):
amount *= (1 + r / compounding_per_year)
ci = amount - principal
return amount, ci
# This produces the same result as Method 1 when used with matching parameters.
Output:

Explaination : This iterative method shows how compounding works step-by-step by applying interest for each period. It’s helpful for learners to understand why the formula works — you repeatedly add interest to the balance for every compounding period. Although slightly less concise than the direct formula, it is conceptually clear and matches the formula when the number of simulated periods equals n * years.
Method 3 – Python program to calculate the compound interest
import math
def compound_interest_continuous(principal, annual_rate_percent, years):
r = annual_rate_percent / 100.0
amount = principal * math.exp(r * years)
ci = amount - principal
return amount, ci
# Example usage:
if __name__ == "__main__":
P = 1000.0
R = 5.0
T = 2.0
A, CI = compound_interest_continuous(P, R, T)
print(f"Amount (continuous) after {T} years: {A:.4f}")
print(f"Compound interest (continuous): {CI:.4f}")
Explaination : Continuous compounding models interest being added an infinite number of times per year and uses the exponential function exp(r*t). This method is important when studying limits — as compounding frequency grows, the discrete formula approaches the continuous result. Use it when you want the theoretical maximum amount for a given rate and time.
Example
Let’s take an example to see numeric values.
- Principal, P = 1000.00
- Annual rate, R = 5% (i.e. r = 0.05)
- Time, T = 2 years
Calculated amounts for different compounding frequencies:
- Annually (n = 1):
- Amount (A) = 1102.5000
- Compound interest (CI) = 102.5000
- Quarterly (n = 4):
- Amount (A) = 1104.4861
- Compound interest (CI) = 104.4861
- Monthly (n = 12):
- Amount (A) = 1104.9413
- Compound interest (CI) = 104.9413
- Daily (n = 365):
- Amount (A) = 1105.1633
- Compound interest (CI) = 105.1633
- Continuous compounding:
- Amount (A) = 1105.1709
- Compound interest (CI) = 105.1709
These values show how more frequent compounding increases the final amount slightly, approaching the continuous compounding limit.
Explanation of the sample code
compound_interestuses Python’s exponentiation operator**to implement the mathematical formula directly — it’s concise and numerically stable for normal inputs.compound_interest_iterativemultiplies the amount period-by-period; this is instructive for learning and matches the direct formula when implemented correctly.compound_interest_continuoususesmath.expfor the continuous compounding case.
Edge cases & tips
- If
yearsis not an integer (e.g., 2.5 years), the formula still works — use float foryears. - If the rate is given as a decimal (0.05) instead of percent (5), either convert it first or adjust the code accordingly.
- For very large
nor very long periods, watch for floating-point rounding; decimal orfractionsmodules can help for exact financial calculations.
Complexity
All methods run in O(1) time (constant) when using the direct formula. The iterative method runs in O(nt) time where nt is the number of compounding periods simulated.
