Python – program to calculate Compound Interest

Python – program to calculate Compound Interest

In this post, we will write a program for how to calculate compound interest in python.
First, let’s see What is compound Interest?
Compound Interest is also called compounding interest which is calculated on the principal (initial principal) that is the addition of interest to the principal sum of a loan or deposit.
The formula for compound interest is:

A = P (1 + r / 100) ^ n

COMPOUND INTEREST = Amount - Principal
Where,
A = final amount including principal
P = Principal
r = Interest rate
n = Time(in years)

Examples:

Input:
Principle (initial amount) = 20000
Rate = 3
Time = 3

Output:
The Compound interest is 1854.54.

Input:
Principle (initial amount) = 10000
Rate = 3
Time = 1

Output:
The compound interest is 250.00.

Let’s see the program for this:
Code: Create a user-defined function which will calculate and print the compound interest value.

  
# define a function for calculating
# compound interest
def compoundinterest(p, r, t): 
    
    # printing given principle amount
    print('Principal is:', p)
    
    # printing given rate
    print('Rate is:', r) 
    
    # printing given time          
    print('Time  is:', t) 
    

   # calculating compound interest
    A = p * ((1 + r / 100) ** t)
    
    CI = A - p
    
    # printing compound interest
    print('The Compound Interest is', CI)


# main code 
if __name__ == "__main__" :
    
    # inputs
    p = 10000
    r = 2
    t = 4
    
    compoundinterest(p, r, t) 

Output:

Principal is: 10000
Rate is: 2
Time  is: 4
The Compound Interest is 824.3215999999993

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds