# Python Program for Compound Interest

Last Updated : 03 Jul, 2023

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

## 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: 3000Enter rate of interest: 5Enter time in years: 3Output: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.

## 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``))``*``*``t  ``# formula for calculating amount ` `ci``=``a``-``p  ``# 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!

## 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)

Previous
Next