# Python Program to Find Compound Interest

The powerful financial idea of compound interest enables an investment to increase exponentially over time. In this article, we'll look at a Python program that computes compound interest depending on the principal sum, the interest rate, and the duration of time. Understanding and putting this program into practice will provide you with a useful tool for evaluating the growth potential of assets.

## What is Compound Interest?

Compound interest is the interest that is calculated on both the initial principal and the interest that has accrued over time. Compound interest, as opposed to simple interest, accounts for the compounding effect that gives result in exponential development. It is a key idea in finance because it enables the quick accumulation of wealth when investments are made over a long period of time.

**The formula for calculating compound interest is as follows:**

A = P * (r/n + 1)^(n*t)

Where A is the total sum (principal plus interest),

P stands for the initial investment's capital.

The yearly interest rate, expressed as a decimal, is r.

N is the number of annual compounding periods.

t = the duration in years

## Why Python to get compound Interest

Python is preferred for calculating compound interest due to its ease of use, readability, and broad library support. Complex financial formulas can be easily implemented thanks to their clear syntax, while libraries like NumPy and pandas make it easier to perform mathematical operations and work with data. Python's extensibility allows for the creation of custom functions and classes, while its variety permits integration with various tools and technologies. Developers have access to a wealth of material and help because of the size and activity of the community. Overall, Python is a trustworthy and effective language for performing precise financial calculations.

Let’s see a few methods to code this problem.

## Using pow function

In this method, we use the pow function to calculate the compound interest and then print it as output. Here is the code for this approach.

**Example:**

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 the user principal = int(input("Enter the principal amount: ")) rate = int(input("Enter the rate of interest: ")) time = int(input("Enter time in years: " )) #Function Call compound_interest(principal,rate,time)

**The output of this code is:**

**Output:**

Enter the principal amount: 1000 Enter the rate of interest: 5 Enter time in years: 5 Compound interest is 276.2815625000003

Compound interest is computed using this code using the principal amount, interest rate, and years provided. It creates a function named compound_interest that accepts these three inputs, applies the formula to determine the compound interest, and outputs the result. The driver code section asks the user for input, uses the supplied values to execute the compound_interest function, and then displays the calculated compound interest.

In this method, we use the formula of compound interest to calculate the compound interest and then print it as output. Here is the code for this approach.

## Without using pow function

**Example:**

def calculate_compound_interest(principal, rate, time): amount = principal * (1 + rate/100)**time interest = amount - principal return amount, interest # Input values principal = float(input("Enter the principal amount: ")) rate = float(input("Enter the interest rate per period (%): ")) time = int(input("Enter the number of periods: ")) # Calculate compound interest final_amount, interest_earned = calculate_compound_interest(principal, rate, time) # Print the results print("Principal amount:", principal) print("Interest rate per period:", rate) print("Number of periods:", time) print("Final amount:", final_amount) print("Interest earned:", interest_earned)

**The output of this code is:**

**Output:**

Enter the principal amount: 1000 Enter the interest rate per period (%): 5 Enter the number of periods: 5 Principal amount: 1000.0 Interest rate per period: 5.0 Number of periods: 5 Final amount: 1276.2815625000003 Interest earned: 276.2815625000003

The principle amount, interest rate per period, and number of periods are entered into this code to calculate compound interest. It includes a function called calculate_compound_interest that, given these three inputs and the provided formulas, computes the amount of interest earned and provides the results. The principle amount, interest rate per period, number of periods, total amount, and interest earned are all printed by the main code section once the user is prompted for input.

## Using for loop

In this method, we use the loop to find the compound interest.

**The code for this approach is:**

**Example:**

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(1000, 5, 5)

**The output of this code is:**

**Output:**

Compound interest is 276.2815625000003

This code uses a loop to repeatedly multiply the principal amount by the interest rate + 1 over the specified time period to calculate compound interest. The inputs for the compound_interest function are the time, interest rate, and principle amount. It updates the amount within the loop for the specified number of iterations after initializing it with the principal. The compound interest is then calculated by deducting the principal from the total. The function is called with specific parameters in the driver code section, and the compound interest is printed.

## Conclusion

Using the Python program to compute compound interest, we can precisely estimate the potential development of your investments over time. This program provides an easy but effective method to comprehend the idea of compound interest and make wise financial decisions. You may improve your investing strategy and reach your financial objectives by utilizing Python and the power of programming.