Python – program to calculate simple interest

Python – program to calculate simple interest

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

Leave a Reply

Your email address will not be published. Required fields are marked *

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds