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