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.
Keep a space where the program output will appear so you can copy-paste the program output after you run it.
r = R / 100.0.n is provided, compute A = P * (1 + r/n)**(n*t).A = P * math.exp(r*t).CI = A - P.# 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: 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.
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.
Let’s take an example to see numeric values.
Calculated amounts for different compounding frequencies:
These values show how more frequent compounding increases the final amount slightly, approaching the continuous compounding limit.
compound_interest uses Python’s exponentiation operator ** to implement the mathematical formula directly — it’s concise and numerically stable for normal inputs.compound_interest_iterative multiplies the amount period-by-period; this is instructive for learning and matches the direct formula when implemented correctly.compound_interest_continuous uses math.exp for the continuous compounding case.years is not an integer (e.g., 2.5 years), the formula still works — use float for years.n or very long periods, watch for floating-point rounding; decimal or fractions modules can help for exact financial calculations.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.