Saturday, September 21, 2024
Google search engine
HomeData Modelling & AIPython Program for Compound Interest

Python Program for Compound Interest

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

Find Compound Interest with Python

Python3




# Python3 program to find compound
# interest for given values.
 
 
def compound_interest(principal, rate, time):
 
    # Calculates compound interest
    Amount = principal * (pow((1 + rate / 100), time))
    CI = Amount - principal
    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.

Compound Interest with Input taking from user

In this method we are going to calculate the compound interest by taking input from the user by using above formula.

Python3




# Python3 program to find compound
# interest for input taking from user.
 
 
def compound_interest(principal, rate, time):
 
    # Calculates compound interest
    Amount = principal * (pow((1 + rate / 100), time))
    CI = Amount - principal
    print("Compound interest is", CI)
 
 
# Driver Code
#Taking input from user.
principal = int(input("Enter the principal amount: "))
rate = int(input("Enter rate of interest: "))
time = int(input("Enter time in years: " ))
#Function Call
compound_interest(principal,rate,time)
 
#This code is contributed by Vinay Pinjala.


Output:

Input:
Enter the principal amount: 3000
Enter rate of interest: 5
Enter time in years: 3
Output:
Compound interest is 472.875

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.

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

Python3




# Python code
# To find compound interest
 
# inputs
p= 1200   # principal 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!

Compound Interest using for loop

Python3




def compound_interest(principal, rate, time):
    Amount = principal
    for i in range(time):
        Amount = Amount * (1 + rate/100)
    CI = Amount - principal
    print("Compound interest is", CI)
# Driver Code
compound_interest(1200, 5.4, 2)
#This code is contributed by Jyothi pinjala


Output

Compound interest is 133.0992000000001

Time complexity: O(1)
Auxiliary Space: O(1) 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments