Q.Write a program that implements a user defined function that accepts Principal Amount, Rate, Time, Number of Times the interest is compounded to calculate and displays compound interest. (Hint: CI=P*(1+r/n)^nt)
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Wrap the compound-interest formula in a user-defined function with four parameters (P, R, T, n); the hint's expression P*(1+r/n)^(nt) yields the total amount, so the interest itself is amount minus principal.
The idea. A user-defined function lets us name a computation once and reuse it with any values. The four inputs the question demands become the four parameters. One subtlety: the hinted formula
A = P * (1 + r/n) ** (n*t) # r as a fraction, i.e. rate/100
is the maturity amount, not the interest. Compound interest is CI = A - P. Since the user enters the rate as a percentage, we divide by n * 100.
def calcCI(principal, rate, time, n):
"""Compute and display compound interest.
rate is % per annum; n = number of times compounded per year."""
amount = principal * (1 + rate / (n * 100)) ** (n * time)
ci = amount - principal
print("Total amount :", round(amount, 2))
print("Compound interest:", round(ci, 2))
p = float(input("Enter principal amount: "))
r = float(input("Enter rate of interest (% per annum): "))
t = float(input("Enter time (in years): "))
n = int(input("Enter number of times interest is compounded per year: "))
calcCI(p, r, t, n)
Key lines
(1 + rate/(n*100)) ** (n*time)— the growth factor appliedn*ttimes (**is Python's power operator).round(..., 2)— money is displayed to 2 decimal places.
Expected output — sample run with P = 10000, R = 8, T = 2, n = 4 (quarterly):
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.