# Python Program to Calculate Compound Interest

In this example, we will write a program that calculates the compound interest in Python. To better understand this example, make sure you have knowledge of the following tutorials:-

Let us have a look at the formula for calculating the simple interest.

Compound Interest = P(1 + R/100)

Where,

P is the principle amount

R is the rate and

T is the time span

^{r}Where,

P is the principle amount

R is the rate and

T is the time span

## Python Program to Calculate Compound Interest

1 2 3 4 5 6 7 8 9 10 11 | def compound_interest(principle, rate, time): result = principle * (pow((1 + rate / 100), time)) return result p = float(input("Enter the principal amount: ")) r = float(input("Enter the interest rate: ")) t = float(input("Enter the time in years: ")) interest = compound_interest(p, r, t) print("Compound interest is %.2f" % interest) |

The output of the above program is:-

Enter the principal amount: 100000

Enter the interest rate: 12

Enter the time in years: 5

Compound interest is 176234.17

Enter the interest rate: 12

Enter the time in years: 5

Compound interest is 176234.17

In this program, the user input is taken using input() function which takes input as a string. But as we are doing the calculation part, we need the input to be a float. So, we did the type-conversion using float() function.

The input from the user is taken as p for the principal amount, t for the time in years and r for the interest rate. The function is defined with def keyword that takes three arguments, principal, rate and time. The formula is applied and result is returned to the calling function and the output is displayed using print() function.

## Recent Comments