Python program to calculate compound interest; In this tutorial, you will learn how to calculate compound interest with and without using function.
Let us have a look at the compound interest rate formula.
A = p * (pow((1 + r / 100), t))
A | = | final amount |
p | = | initial principal balance |
r | = | interest rate |
t | = | number of time periods elapsed |
Python Program to Compute Compound Interest
Now let’s see each python program for calculate or compute compound interest; as shown below:
- Python program to compute compound interest
- Python Program to Calculate Compound Interest using Function
1: Python program to compute compound interest
- Use a python input() function in your python program that takes an input (principal amount, rate of interest, time) from the user.
- Next, calculate the compound interest using this p * (pow((1 + r / 100), t)) formula.
- After the end program, print the compound interest.
#Python program to compute compound interest
p = float(input("Enter the principal amount : "))
t = float(input("Enter the number of years : "))
r = float(input("Enter the rate of interest : "))
#compute compound interest
ci = p * (pow((1 + r / 100), t))
#print
print("Compound interest : {}".format(ci))
Output
Enter the principal amount : 1000 Enter the number of years : 2 Enter the rate of interest : 5 Compound interest : 1102.5
2: Python Program to Calculate Compound Interest using Function
- Define a function in your python program that accepts the argument and compute compound interest by using this p * (pow((1 + r / 100), t)) formula.
- Use a python input() function in your python program that takes an input (principal amount, rate of interest, time) from the user.
- Next, call a compute interest function with specific arguments.
- After the end program, print the compound interest.
#Python program to compute compound interest using function
def compoundInterest(p, r, t):
ci = p * (pow((1 + r / 100), t))
return ci
p = float(input("Enter the principal amount : "))
t = float(input("Enter the number of years : "))
r = float(input("Enter the rate of interest : "))
#call compound interest
ci = compoundInterest(p, r, t)
#print
print("Compound interest : {}".format(ci))
Output
Enter the principal amount : 1000 Enter the number of years : 2 Enter the rate of interest : 5 Compound interest : 1102.5
Recommended Python Programs
Recommended:-Leap Year Program in Python
Recommended:-Python Program to Print Star Pattern
Recommended:-Number Pattern Programs in Python
Recommended:-Python Abs() Function: For Absolute Value
Recommended:-Python: Program to Find Power of Number
Recommended:-Python Program to Reverse a Numbers
Recommended:-Python Program to Find Smallest/Minimum of n Numbers
Recommended:-Python Program to Find Largest/Maximum of n Numbers
Recommended:-Python Program to Print Numbers Divisible by 3, 5, 7
Recommended:-Python Program to Print Prime Number 1 to N
Recommended:-How to Find Square of Number in Python
Recommended:-Python Program to Calculate Cube of Number
Recommended:-BMI (Body Mass Index) Calculator in Python
Recommended:-Python Random Number Generator Code