Python Program to Find Compound Interest.

In this article, we will discuss Python code for calculating Compound Interest. But before moving to the code section let us understand the process of calculating Compound Interest.


What is Compound Interest?

Compound Interest is the interest calculated on the original principal of a loan or deposit, as well as on the accumulated interest of previous periods. This means that the interest earned in a given period is added to the principal, and the interest for the next period is calculated based on the increased principal. This results in a faster accumulation of interest over time compared to simple interest.


The formula for calculating compound interest is: Compound Interest = Principal * (1 + Rate / n)^(nt)

  • The principal is the original amount of the loan or deposit.
  • Rate is the interest rate, expressed as a decimal.
  • n is the number of times the interest is compounded in a year.
  • t is the number of years for which the loan or deposit is held.

For example, if you deposit $1000 at an interest rate of 5% compounded annually for 2 years, the compound interest earned would be $1000 * (1 + 0.05 / 1)^(2 * 1) = $1050.25.


Python Program to calculate the Compound Interest.

def compound_interest(principal, rate, n, t):
    return principal * (1 + rate / n) ** (n * t)

# take input for the principal, rate, number of times compounded, and time
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
n = float(input("Enter the number of times the interest is compounded per year: "))
t = float(input("Enter the time period (in years): "))

# convert the interest rate from a percentage to a decimal
rate = rate / 100

# calculate the compound interest
interest = compound_interest(principal, rate, n, t) - principal

# print the result
print("The compound interest is", interest)
Output:
Enter the principal amount: 150000
Enter the rate of interest: 8
Enter the number of times the interest is compounded per year: 7
Enter the time period (in years): 6
The compound interest is 91752.18133848405
  • Time Complexity: O(1)
  • Space Complexity: O(1)
Read more:

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS