Signup/Sign In

Python Program to Calculate Compound Interest

In this tutorial, we will see how to write a program to find Compound Interest when the principal amount, rate of interest, and time are given by the user. Compound interest is the addition of interest to the principal amount, or in other words, interest on interest. This is different from Simple Interest which is based only on the principal amount.

The formula for calculating compound interest is given below.

Formula

compound interest

Take a look at the sample input and output of the program which will calculate compound interest on a principal amount of Rs. 1000 at the rate of 2% for 1 year.

Input:

P= 1000

R= 2

T= 1

Output:

C I= 20.0

Algorithm

Step 1 - Define function compound_interest() to calculate CI

Step 2 - Declare variable amt to store and calculate compound interest

Step 3 - Use pow() function for exponential calculation according to the formula

Step 4 - Calculate interest by subtracting compound interest from the principal amount

Step 5- Return CI

Step 6- Print the value returned by the function

Python Program

Look at the program to understand the implementation of the above-mentioned approach.

import math  
def compound_interest(p,r,t):   
    amt = p * (math.pow((1 + (r/100)),t))
    print("Compound Amount: ",amt)
    CI = amt - p
    return CI

p=float(input("Enter Principal Amount: "))
r=float(input("Enter Rate of Interest: "))
t=float(input("Enter Time Period in years: "))

print("Compound interest is",compound_interest(p,r,t))


Enter Principal Amount: 100000
Enter Rate of Interest: 12
Enter Time Period in years: 5
Compound Amount: 176234.16832000008
Compound interest is 76234.16832000008

Conclusion

In this tutorial, we learned how to calculate Compound Interest using the Python programing language. We used the formula for calculating Compound Interest. To perform exponential calculations we used a built-in math function pow(). We took inputs of the parameters- principal, interest, and time and passed these values in the function compound_interest(). We displayed the value of CI which was calculated and returned by the function.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.