Question 12 of 17intermediate🔧 ApplyLong Answer4 marks

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)

Correct Answer

Program to calculate Compound Interest using user-defined function

def compound_interest(P, r, t, n): # P = Principal Amount # r = Rate of interest # t = Time period # n = Number of times interest is compounded

# Calculate compound interest using formula: CI = P*(1+r/n)^(n*t)
CI = P * (1 + r/n) ** (n * t)
return CI

Main program

Accepting inputs from user

P = float(input("Enter Principal Amount: ")) r = float(input("Enter Rate of Interest: ")) t = float(input("Enter Time Period: ")) n = int(input("Enter Number of times interest is compounded: "))

Calling the user-defined function

CI = compound_interest(P, r, t, n)

Displaying the result

print("Compound Interest =", CI)

Exercise: SUGGESTED LAB. EXERCISES | Q: 5 | (Page 30)
For More Understanding

Explanation

This program demonstrates the use of user-defined functions in Python. The function compound_interest() accepts four parameters (Principal, Rate, Time, Number of times compounded) and calculates compound interest using the formula CI = P*(1+r/n)^(nt). The ** operator is used for exponentiation in Python. The main program accepts user inputs, calls the function, and displays the result.

Solution Steps

  1. Step 1: Define a user-defined function compound_interest() with parameters P, r, t, n

  2. Step 2: Inside the function, apply the formula CI = P * (1 + r/n) ** (n * t) using the exponentiation operator **

  3. Step 3: Return the calculated compound interest value

  4. Step 4: In the main program, accept Principal Amount, Rate, Time, and Number of times compounded from user using input()

  5. Step 5: Call the function with user inputs and display the result using print()