# Control Structures (Compound Interest)

Back To The Index

Untitled1

Compound interest can be calculated using the formula $$A = P\times(1 + \frac{r}{n})^{nt}$$ Where

• P : Principal amount (P)
• r : Annual interest rate
• t : Time factor (t)
• n : Number of compound periods per year.

Following program prompts user to enter principal amount and time of loan in years. Its uses interest rate as $2.5%$ APR at compounding rate of n = 4 (quarterly compounded) and prints the final amount payable at the end of loan term.

In :
def main():

# Fixed parameters
r = 0.025
n = 4

#User supplied parameters
P = input('Enter principal amount: ')
t = input('Enter duration of loan in years: ')

if not (P.isnumeric() and t.isnumeric()):
print ('Please enter numeric values only')
elif (float(t)<=1 or float(P)<=0):
print ('The principal amount P must be more than 0')
print ('The time duration must be more than 1')
else:
P = float(P)
t = float(t)
A = P * pow((1 + r/n),(n*t))
print ('The total amount payable is: ',A)

if __name__ == '__main__':
main()

Enter principal amount: 100
Enter duration of loan in years: 2
The total amount payable is:  105.11075292222539

Back To The Index