Skip to content
Home » Python Program to Calculate Compound Interest using Function

Python Program to Calculate Compound Interest using Function

Learn about Python Program to Calculate Compound Interest using Function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Python Program to Calculate Compound Interest using Function

# Program to calculate compound interest using function

# user-defined function
def compound_interest(principal, rate, time, number):
    amount = principal * pow( 1+(rate/number), number*time)
    return amount

# inputs from user
principal = float(input('principal amount: '))
rate = float(input('interest rate: '))
time = float(input('time (in years): '))
number = float(input('Number of times that interest is compounded per year: '))

# conversion rate
rate = rate/100

# function call
amount = compound_interest(principal, rate, time, number)

# calculating compound interest
ci = amount - principal

# printing result
print('Compound interest = %.2f' %ci)
print('Total amount = %.2f' %amount)

Output:

principal amount: 2000
interest rate: 2
time (in years): 5
Number of times that interest is compounded per year: 2
Compound interest = 209.24
Total amount = 2209.24

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Program to Calculate Compound Interest in Python