Tuesday, 17 December 2024

Day 41: Python program to calculate simple interest

 


def calculate_simple_interest(principal, rate, time):

    simple_interest = (principal * rate * time) / 100

    return simple_interest

principal = float(input("Enter the principal amount: "))

rate = float(input("Enter the rate of interest: "))

time = float(input("Enter the time in years: "))

simple_interest = calculate_simple_interest(principal, rate, time)

print(f"The Simple Interest is: {simple_interest}")

#source code --> clcoding.com 

Code Explanation:

Function Definition
def calculate_simple_interest(principal, rate, time):
    simple_interest = (principal * rate * time) / 100
    return simple_interest
def: This keyword is used to define a function.
calculate_simple_interest: The name of the function. It describes its purpose—to calculate simple interest.

Parameters:
principal: The initial amount of money (loan or deposit).
rate: The rate of interest (as a percentage).
time: The time for which the money is borrowed or invested, in years.

Formula for Simple Interest:
Simple Interest = Principal × Rate × Time/100
Multiply the principal amount by the rate and time.
Divide the result by 100 to calculate the interest.
return simple_interest: Returns the calculated interest value back to where the function is called.

2. User Input
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time in years: "))
input(): Accepts user input as a string.
float(): Converts the input string into a floating-point number to perform arithmetic operations.

Prompts the user to input:
Principal: The starting loan or deposit amount.
Rate: The interest rate (percentage).
Time: The duration (in years) for which interest is calculated.

Example Input:
Enter the principal amount: 1000  
Enter the rate of interest: 5  
Enter the time in years: 2  

3. Function Call
simple_interest = calculate_simple_interest(principal, rate, time)
The calculate_simple_interest() function is called with the user-provided values for principal, rate, and time.
The calculated interest is stored in the variable simple_interest.
Example Calculation (using input values from above):
Simple Interest =1000×5×2/100 =100

4. Output
print(f"The Simple Interest is: {simple_interest}")
f-string: A formatted string used to print variables within a string.
{simple_interest}: Inserts the calculated interest value into the output message.

Example Output:
The Simple Interest is: 100.0

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (90) AI (37) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (184) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (135) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (19) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (5) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (959) Python Coding Challenge (402) Python Quiz (56) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses