wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

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)

Open in App
Solution

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

Program:

#Defining the function
def calculate (p, r, t, n=1) :
#calculation 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 to half-yearly ) : 4
The compound interest will be 4859.47


flag
Suggest Corrections
thumbs-up
0
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Application of Compound Interest
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon