English

Write a program that implements a user-defined function that accepts Principal Amount, Rate, Time, and Number of Times the interest is compounded to calculate and displays compound interest. - Computer Science (Python)

Advertisements
Advertisements

Question

Write a program that implements a user-defined function that accepts Principal Amount, Rate, Time, and Number of Times the interest is compounded to calculate and displays compound interest. (Hint: CI = P*(1 + r/n)nt)

Answer in Brief
Advertisements

Solution

The hint given in the question has a mistake. The formula (P*(1+R/N)NT) will give us the amount, not the Compound Interest.

Program:
#Defining the function
def calculate(p,r,t,n=1):
    #calculating the amount
    amount = p * (1 + r/(n * 100))**(n * t)
    #calculating the compound interest and rounding it off to 2 decimal places
    ci = round(amount - p, 2)
    #displaying the value of the compound interest
    print("The compound interest will be ",ci)

#asking the principal, rate, and time
p = float(input("Enter the principal amount: "))
r = float(input("Enter the rate of interest: "))
t = float(input("Enter total time: "))
n = int(input("Number of times the interest is compounded in a year\n(e.g.Enter 1 for yearly, 2 for half-yearly): "))
if(n > 365):
    calculate(p,r,t,n)
else:
    print("Maximum number of times an interest is compounded can not be more than 365")

​OUTPUT:
Enter the principal amount: 10000
Enter the rate of interest: 8
Enter total time:  5
Number of times the interest is compounded in a year
(e.g. Enter 1 for yearly, 2 for half-yearly): 4
The compound interest will be  4859.47
shaalaa.com
  Is there an error in this question or solution?
Chapter 7: Functions - Exercise [Page 172]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 7 Functions
Exercise | Q 5. | Page 172
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×