In this post, we will write a program for how to calculate simple interest in Python.
First, let’s see What is Simple Interest?
Simple interest is an interest calculated on the principal amount of a loan or the original contribution to a savings account.
Simple Interest: I = (P * R * T) / 100 Where, I = Interest earned after T years. P = Money borrowed or invested. R = Annual rate of interest. T = The length of time you borrow or invest.
Examples:
Input:
Principle=20000
Rate=2
Time=5
Output:
The simple interest is 2000.Input:
Principle=10000
Rate=5
Time=3
Output:
The simple interest is 1500.
Let’s see the program for this:
Code version 1: Create a user defined function which will calculate and print the simple interest value.
# define a function for calculating # simple interest def simple_interest(p, r, t): # printing given principle amount print('Principal is:', p) # printing given rate print('Rate is:',r) # printing given time print('Time is:', t) # calculating simple interest s = (p * t * r) / 100 # printing simple interest print('The Simple Interest is', s) # main code if __name__ == "__main__": # inputs p = 10000 r = 6 t = 4 # function call simple_interest(p, r, t)
Output:
Principal is: 10000 Rate is: 6 Time is: 4 The Simple Interest is 2400.0
Code version 2: Create a user defined one line anonymous function which will calculate and return the simple interest value using a lambda statement.
# main code if __name__ == "__main__": # inputs p = 10000 r = 6 t = 4 # printing given principle amount print('Principal is:', p) # printing given rate print('Rate is:',r) # printing given time print('Time is:', t) # create a one line function # using lambda statement # for calculating # simple interest simple_interest = lambda p, r, t : (p * r * t) / 100 # function call and # print the return # value print('The Simple Interest is', simple_interest(p, r, t))
Output:
Principal is: 10000 Rate is: 6 Time is: 4 The Simple Interest is 2400.0