## Introduction: Python Program - Compound Interest WITH Monthly Contributions/Deposit

Program to calculate compound interest with monthly contribution at the end of the month.

Formula taken from TheCalculatorSite.com:

Compound interest for principal:

P(1+r/n)^(nt)

Future value of a series:

PMT × (((1 + r/n)^nt - 1) / (r/n))

## Step 1: Get Data From User

__Ask the user to put in the required data:__

principalinput = input("Enter principal: ")

annualrateinput = input("Enter annual rate: ")

numberoftimescompoundedinput = input("Enter number of times that the interest is compounded per year: ") yearsinput = input("Time in years: ")

monthlycontributioninput = input("Enter monthly contribution amount: ")

## Step 2: Convert Data

__Data is entered as a strings which must be converted into integers__

# Convert entered input from strings into integers

principal = int(principalinput)

annualrate = (int(annualrateinput))/100

numberoftimescompounded = int(numberoftimescompoundedinput)

years = int(yearsinput)

monthlycontribution = int(monthlycontributioninput)

## Step 3: Calculate the Compound Interest Plus the Principal

__First, calculate the compound interest plus the principal:__

# calculate compound interest plus the principal

preliminarynumber = (1 + (annualrate/numberoftimescompounded))

# print ("Preliminary number:", preliminarynumber)

raisedtopower = (numberoftimescompounded * years)

# print ("Raised to power:", raisedtopower)

compoundinterestplusprincipal = principal * (preliminarynumber**raisedtopower)

print("The compound interest plus the principal is: ", compoundinterestplusprincipal)

## Step 4: Calculate the Future Value

__Then calculate the future value with deposits__

# Now calculate the future value with deposits made at the end of the period

# Using formula: Monthly Payment × ( ( ( (1 + r/n)^(nt) ) - 1 ) / (r/n) )

# r = annual interest rate

# n = number of compounds per period (usually in months)

# t = time the money is invested (usually in years)

oneplus = (1+(annualrate/numberoftimescompounded))

raisedtopower2 = ((numberoftimescompounded*years))

ratedividedbynumberoftimes = annualrate/numberoftimescompounded

halfdone = (((oneplus**raisedtopower2)-1)/ratedividedbynumberoftimes)

futurevaluewithdeposits = monthlycontribution*halfdone

print ("Future value with deposits: ",futurevaluewithdeposits)

totalamount = compoundinterestplusprincipal + futurevaluewithdeposits

print ("Total Amount:", totalamount)

## Step 5: Code

# Program to calculate compound interest with monthly contribution at end of month

# First calculate the compound interest for principal using formula: A = P (1 + r/n)**(nt)

# r = annual interest rate

# n = number of compounds per period (usually in months)

# t = time

principalinput = input("Enter principal: ")

annualrateinput = input("Enter annual rate: ")

numberoftimescompoundedinput = input("Enter number of times that the interest is compounded per year: ")

yearsinput = input("Time in years: ")

monthlycontributioninput = input("Enter monthly contribution amount: ")

# Convert entered input from strings into integers

principal = int(principalinput)

annualrate = (int(annualrateinput))/100

numberoftimescompounded = int(numberoftimescompoundedinput)

years = int(yearsinput)

monthlycontribution = int(monthlycontributioninput)

print ("The principal entered is: ", principal)

print ("The annual rate in decimal form is: ", annualrate)

print ("The number of times it will be compounded per year is: ", numberoftimescompounded)

print ("The number of years it will be compounded: ", years)

print ("The monthly contribution is: ", monthlycontribution)

# calculate compound interest plus the principal

preliminarynumber = (1 + (annualrate/numberoftimescompounded))

# print ("Preliminary number:", preliminarynumber)

raisedtopower = (numberoftimescompounded * years)

# print ("Raised to power:", raisedtopower)

compoundinterestplusprincipal = principal * (preliminarynumber**raisedtopower)

print("The compound interest plus the principal is: ", compoundinterestplusprincipal)

# Now calculate the future value with deposits made at the end of the period

# Using formula: Monthly Payment × ( ( ( (1 + r/n)^(nt) ) - 1 ) / (r/n) )

# r = annual interest rate

# n = number of compounds per period (usually in months)

# t = time the money is invested (usually in years)

oneplus = (1+(annualrate/numberoftimescompounded))

raisedtopower2 = ((numberoftimescompounded*years))

ratedividedbynumberoftimes = annualrate/numberoftimescompounded

halfdone = (((oneplus**raisedtopower2)-1)/ratedividedbynumberoftimes)

futurevaluewithdeposits = monthlycontribution*halfdone

print ("Future value with deposits: ",futurevaluewithdeposits)

totalamount = compoundinterestplusprincipal + futurevaluewithdeposits

print ("Total Amount:", totalamount)

## 2 Comments

Tip 1 year ago

Hi Matt , really nice of you to share this, one small comment, you have a small mistake where you try to convert the string input from the rate , because you are using an int, and then dividing by 100, this will produce an error, which is fixed by converting to float instead, and then dividing by 100.

5 years ago

Forgot to add the code! Added Nov 14, 2017.