Python programs to calculate compound interest have been shown here. Both of the iterative and recursive approaches have been covered. Computing compound interest using formula has also been shown here.
The following section covers the Python programs to calculate compound interest.
Page content(s):
2. Program: Iterative approach
3. Program: Recursive approach
Additional content(s):
1. Python Program & output to calculate compound interest using formula
################################################################## # alphabetacoder.com # Python program to calculate compound interest using formula ################################################################## # take input of initial principal amount, # interest rate, periodicity of compounding and year p = float(input("Enter the principal balance = ")) r = float(input("Enter the interest rate = ")) n = int(input("Enter compound frequency / year = ")) t = int(input("Enter the year = ")) # calculate compounded value amount = p * (1 + r/(100 * n))**(n*t) # find the compound interest ci = amount - p; # display result upto 2 decimal places print("Compound interest = ",round(ci, 2))
Output
Enter the principal balance = 15000
Enter the interest rate = 7
Enter compound frequency / year = 4
Enter the year = 8
Compound interest = 11133.2
2. Python Program & output to calculate compound interest using loop (Iteration)
####################################################################### # alphabetacoder.com # Python program to calculate compound interest using loop (Iteration) ####################################################################### # take input of initial principal amount, # interest rate, periodicity of compounding and year p = float(input("Enter the principal balance = ")) r = float(input("Enter the interest rate = ")) n = int(input("Enter compound frequency / year = ")) t = int(input("Enter the year = ")) # initialize amount = p; # calculate compounded value using loop for i in range(n*t): amount = amount + amount * (r / (n * 100)) # find the compound interest ci = amount - p # display result upto 2 decimal places print("Compound interest = ",round(ci, 2))
Output
Enter the principal balance = 110000
Enter the interest rate = 11.25
Enter compound frequency / year = 1
Enter the year = 8
Compound interest = 148102.88
3. Python Program & output to calculate compound interest using recursion
################################################################## # alphabetacoder.com # Python program to calculate compound interest using Recursion ################################################################## # recursive function to compute compound interest def compound_interest(p0, p, r, n, t, itr): # if number of total no of compound reached if itr == n * t: return (p - p0) # calculate interest interest = p * (r / (n * 100)) # call function return compound_interest(p0, p + interest, r, n, t, itr + 1) def main(): # take input of initial principal amount, # interest rate, periodicity of compounding and year p = float(input("Enter the principal balance = ")) r = float(input("Enter the interest rate = ")) n = int(input("Enter compound frequency / year = ")) t = int(input("Enter the year = ")) # find the compound interest ci = compound_interest(p, p, r, n, t, 0) # display result upto 2 decimal places print("Compound interest = ",round(ci, 2)) # driver code main()
Output
Enter the principal balance = 90000
Enter the interest rate = 7
Enter compound frequency / year = 4
Enter the year = 20
Compound interest = 270575.27