Skip to content
Related Articles

Related Articles

Python Program for compound interest

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 11 Aug, 2022
Improve Article
Save Article

Let us discuss the formula for compound interest. The formula to calculate compound interest annually is given by: 

A = P(1 + R/100) t 

Compound Interest = A – P 

Where, 
A is amount 
P is the principal amount 
R is the rate and 
T is the time span

Example: 

Input: Principle (amount): 1200, Time: 2, Rate: 5.4
Output: Compound Interest = 133.099243

Example

Python3




# Python3 program to find compound
# interest for given values.
 
 
def compound_interest(principle, rate, time):
 
    # Calculates compound interest
    Amount = principle * (pow((1 + rate / 100), time))
    CI = Amount - principle
    print("Compound interest is", CI)
 
 
# Driver Code
compound_interest(10000, 10.25, 5)

Output

Compound interest is 6288.946267774416

Time Complexity: O(1) since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant

Method 2:  Finding compound interest of given values without using pow() function.

Python3




# Python code
# To find compound interest
 
# inputs
p= 1200   # principle amount
t= 2      # time
r= 5.4    # rate
# calculates the compound interest
a=p*(1+(r/100))**# formula for calculating amount
ci=a-# compound interest = amount - principal amount
# printing compound interest value
print(ci)

Output

133.0992000000001

Time Complexity: O(1) since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant

Please refer complete article on Program to find compound interest for more details!


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!