Compound Interest Program 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**

**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.

## Python Program to compute Compound Interest

This is the simplest and easiest way to calculate the compound interest program in Python.

Program description:- **Write a Python program to calculate the compound interest**

```
# 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

In this program, Inputs (**principal amount, interest rate, time, and number of times that interest is compounded per year**) will be provided by the user. 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 formula.

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

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

## Compound Interest Python 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!