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
def compound_interest(principal, rate, time):
Amount = principal * ( pow (( 1 + rate / 100 ), time))
CI = Amount - principal
print ( "Compound interest is" , CI)
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
def compound_interest(principal, rate, time):
Amount = principal * ( pow (( 1 + rate / 100 ), time))
CI = Amount - principal
print ( "Compound interest is" , CI)
principal = int ( input ( "Enter the principal amount: " ))
rate = int ( input ( "Enter rate of interest: " ))
time = int ( input ( "Enter time in years: " ))
compound_interest(principal,rate,time)
|
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
p = 1200
t = 2
r = 5.4
a = p * ( 1 + (r / 100 )) * * t
ci = a - p
print (ci)
|
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)
compound_interest( 1200 , 5.4 , 2 )
|
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 geeks!
Last Updated :
03 Jul, 2023
Like Article
Save Article