How to calculate compound interest in Python | **Compound interest** is a loan or interest on a deposit based on both the initial principal and the accumulated interest from the previous periods. Compound interest is calculated by multiplying the initial principal amount by one plus the annual interest rate raised to the number of compound periods minus one. After that, the total initial amount of the loan is then subtracted from the resulting value.

So, to calculate the annual compound interest, multiply the original amount of your investment or loan, or principal, by the annual interest rate. Add that amount to the principal, then multiply by the interest rate again to get the second year’s compounding interest.

**Compound interest formula in Python:-**

**A = P(1 + r/n) ^ (n * t)**

Where,**A** is the future value of the investment/loan, including interest**P** is the principal amount**r** is the annual rate of interest**n** is the number of times that interest is compounded per unit t and**t** is the time the money is invested (number of years)

The above formula gives the total amount. To find the compound interest use,**Compound interest = A – P**

**Mathematically,**

Input :- P = 5000

r = 5/100 = 0.05

n = 12

t = 10

If we plug those figures into the formula, we get the following

A = 5000 (1 + 0.05 / 12) ^ (12 * 10) = 8235.05

Compound Interest = A – P = 8235.05 – 5000 = 3235.05

Total Amount is 8235.05 and compound Interest is 3235.05

Now let’s see how we can implement **compound interest calculator** in Python.

## Write a Program to Calculate Compound Interest in Python

This is the simplest and easiest way to calculate compound interest. We will take Inputs (**principal amount, interest rate, time, and number of times that interest is compounded per year**) while declaring the variables. Then, calculate the compound interest using the formula and finally, the amount will be displayed on the screen.

Program description:- **Write a Python program to find compound interest on an amount. (you should decide the amount and interest rate; print the amount after each year.)**

```
principal = 1000
rate = 10
time = 5
number = 6
rate = rate/100
amount = principal * pow( 1+(rate/number), number*time)
ci = amount - principal
print('Compound interest = %.2f' %ci)
print('Total amount = %.2f' %amount)
```

Output:-

Compound interest = 641.94

Total amount = 1641.94

## Python Program to compute Compound Interest

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

Program description:- **Write a Python program to compute the future value of a specified principal amount, rate of interest, and number of years.**

```
# Python program to find compound interest
# store the inputs
principal = float(input('Enter principal amount: '))
rate = float(input('Enter the interest rate: '))
time = float(input('Enter time (in years): '))
number = float(input('Enter the number of times that
interest is compounded per year: '))
# convert rate
rate = rate/100
# calculate total amount
amount = principal * pow( 1+(rate/number), number*time)
# calculate compound interest
ci = amount - principal
# display result
print('Compound interest = %.2f' %ci)
print('Total amount = %.2f' %amount)
```

Output for the different test-cases:-

Enter principal amount: 10000*E*nter the interest rate: 6

Enter time (in years): 5

Enter the number of times that interest is compounded per year: 6

Compound interest = 3478.49

Total amount = 13478.49

Enter principal amount: 50000*E*nter the interest rate: 3.8

Enter time (in years): 10

Enter the number of times that interest is compounded per year: 12

Compound interest = 23070.34

Total amount = 73070.34

Inputs are scanned using the input() function. input() method reads user input as a string. So, we need to convert it to float first. The principal amount, interest rate, time, and number of times that interest is compounded per year are stored.

```
principal = float(input('Enter principal amount: '))
rate = float(input('Enter the interest rate: '))
time = float(input('Enter time (in years): '))
number = float(input('Enter the number of times that
interest is compounded per year: '))
```

Now, convert the rate of interest to a percentage and calculate the compound interest using the formula.

```
rate = rate/100
amount = principal * pow( 1+(rate/number), number*time)
```

Then, compound interest and the total amount is displayed using the print() function.

## Python Program to Calculate Compound Interest using Function

This is a different method to calculate compound interest in python. We can also take the help of the** user-defined function**. A function is a block of code that performs a specific task.

```
# Python program to calculate compound interest using function
def compound_interest(principal, rate, time, number):
# calculate total amount
amount = principal * pow( 1+(rate/number), number*time)
return amount;
# store the inputs
principal = float(input('Enter principal amount: '))
rate = float(input('Enter the interest rate: '))
time = float(input('Enter time (in years): '))
number = float(input('Enter the number of times that
interest is compounded per year: '))
# convert rate
rate = rate/100
# calling function
amount = compound_interest(principal, rate, time, number)
# calculate compound interest
ci = amount - principal
# display result
print('Compound interest = %.2f' %ci)
print('Total amount = %.2f' %amount)
```

Output:-

Enter principal amount: 100000*E*nter the interest rate: 7

Enter time (in years): 20

Enter the number of times that interest is compounded per year: 22

Compound interest = 304619.71

Total amount = 404619.71

In this program, we will be the first defined function. Inputs are scanned using the input() function. The principal amount, interest rate, time, and number of times that interest is compounded per year are stored. Then call the function and print the compound interest and total amount.

Also See:- Simple Interest Program in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!