## In this Tutorial -:

## Understanding the Problem

Compound interest is a financial concept used to calculate the interest earned on a principal amount over multiple compounding periods. The formula to calculate compound interest is:

**Compound Interest (CI) = Principal Amount * [(1 + (Rate of Interest / 100)) ** Time] – Principal Amount**

In this tutorial, we’ll develop a Python program to calculate compound interest by taking user inputs for the principal amount, rate of interest, time, and compounding frequency.

## Developing the Algorithm

-> Take user input for the principal amount, rate of interest, time period, and compounding frequency.

-> Calculate the compound interest using the provided formula.

-> Display the calculated compound interest to the user.

## Writing the Python Code

```
# Function to calculate compound interest
def calculate_compound_interest(principal, rate, time, frequency):
amount = principal * (1 + (rate / (100 * frequency))) ** (frequency * time)
compound_interest = amount - principal
return compound_interest
# Main function
def main():
# Input principal amount, rate of interest, time period, and compounding frequency
principal_amount = float(input("Enter the principal amount: "))
rate_of_interest = float(input("Enter the rate of interest: "))
time_period = float(input("Enter the time period (in years): "))
compounding_frequency = int(input("Enter the compounding frequency per year: "))
# Calculate compound interest
compound_interest = calculate_compound_interest(principal_amount, rate_of_interest, time_period, compounding_frequency)
# Display the result
print("Compound Interest:", compound_interest)
if __name__ == "__main__":
main()
```

**O/P**

Enter the principal amount: 5000

Enter the rate of interest: 5.5

Enter the time period (in years): 2

Enter the compounding frequency per year: 12

Compound Interest: 579.99

## Time Complexity

The time complexity of this program is constant (O(1)) because the execution time is not dependent on the input values. The program will always execute a fixed number of operations regardless of the values entered by the user.

## Code Explanation

We define a function *calculate_compound_interest* that takes four arguments: *principal, rate, time, *and *frequency*. Inside the function, we calculate the compound interest using the provided formula and return the result.

The *main* function is defined to take user input for the principal amount, rate of interest, time period, and compounding frequency.

User inputs are converted to floating-point numbers using *float(input(…))* and an integer using *int(input(…))*.

The *calculate_compound_interest* function is called with the user-provided inputs, and the result is stored in the *compound_interest* variable.

Finally, the calculated compound interest is displayed to the user.

## Dry Run

Let’s perform a dry run of the program with the following inputs:

Principal amount = 5000

Rate of interest = 5.5

Time period = 2

Compounding frequency = 12 (monthly compounding)

Execution:

User enters the principal amount: 5000

User enters the rate of interest: 5.5

User enters the time period (in years): 2

User enters the compounding frequency per year: 12

The *calculate_compound_interest* function is called with principal_amount = 5000, rate_of_interest = 5.5, time_period = 2, and compounding_frequency = 12.

The compound interest is calculated as follows:

```
amount = 5000 * (1 + (5.5 / (100 * 12))) ** (12 * 2) ≈ 5579.9775216
compound_interest = 5579.9775216 - 5000 ≈ 579.99 (after rounding)
```

The calculated compound interest (approximately 579.99) is displayed to the user.

The program successfully calculates and displays the compound interest based on the user inputs.