In this tutorial, we will learn how to write a Python program to find compound interest for a given value. Before we proceed with writing the program, let’s first under the basics of compound interest.

## Compound interest

The addition of interest in the given principal value for the deposit or loan is called compound interest, and it is also known as interest on interest. Compound interest is basically the result of reinvesting the gained interest on principal value, rather than taking or paying it out, which will result in the next period of interest; the paid interest will be added to the principal amount.

Let’s compare compound interest and simple interest. We can see that, in simple interest, there is no compounding of interest on principal values, and gained interest remains the same on every cycle.

Fact: Compound interest is the standard method of interest calculation in banking, finance, and economics.

The formula for compound interest:

The general mathematical formula for calculating compound interest on a given principal value, i.e., V, is given below:

Total Amount = P(1+r/100)tp

In the above formula, the variables we have used in it can be described as given below:

Total amount = Principal value + Compound interest gained

P = Principal value

tp = Time period for which Principal value is invested

r = Rate of interest

## Code Implementation

Till now, we have learned the basics of compound interest and how important it is in our daily life. We have also learned about the basic formula that we can use for calculating the Compound interest for a certain principal value.

Now, in this section, we will write a Python program to calculate the compound interest for a given certain value. For writing the required Python program, we have to follow certain steps, which are given below:

Step 1: We will take the principal amount as user input.

Step 2: Then, we ask the user to set the rate of interest and the time period for which the principal amount is invested.

Step 3: After taking all the three required variables from the user, we will use the compound interest formula, i.e., “Total Amount = P(1+r/100)tp,” on these variables in the program.

Step 4: We will store the result in a ‘Result variable’.

Step 5: In the last, we will print the compound interest as a result in the output of the program.

Now, look at the following Python program to better understand the implementation of the above given steps:

Example –

```
# Using default function
def compound_rate(PV, CRate, tp):
# Using CI formula
TotalAmount = PV * (pow ((1 + CRate / 100), tp))
# Calculating CI
CInterest = TotalAmount - PV
# Printing CI as result in output
print("Total return value after completion of given time period: ", TotalAmount)
print("Compound interest gained on given amount is", CInterest)
# Taking principal amount value from the user
PV = float(input("Enter the principal amount: "))
CRate = float(input("Enter the rate for compound interest: ")) # taking interest rate value
tp = float(input("Enter the time period for which principal is invested: ")) # taking time period value
# Calling out CI function
compound_rate(PV, CRate, tp)
```

Output:

```
Enter the principal amount: 600000
Enter the rate for compound interest: 2.7
Enter the time period for which principal is invested: 20
Total return value after completion of given time period: 1022257.0687807774
Compound interest gained on given amount is 422257.0687807774
```

Explanation –

After running the above program, we have given three required variables, i.e., PV = 600000, Crate = 2.7, and tp = 20; we got the total compound interest and total value (1022257.0687807774) that we will get on the given principal value and printed the result in the output. We can calculate the compound interest on any given amount for any rate of interest and for any time period using this program.

## Leave a Review