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 = 3Output:
The Compound interest is 1854.54.Input:
Principle (initial amount) = 10000
Rate = 3
Time = 1Output:
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