Python program to calculate simple interest with user-provided values:
In this tutorial, we will learn how to calculate simple interest in Python
. The program will take the values as inputs from the user. Simple interest is calculated on the main amount of a loan. This amount is also known as the principal amount.
To calculate the interest value, we need to know the principal amount, the rate of interest, and the time period. This is a beginner-level program and you will learn how to read user inputs, how to do basic arithmetic operations, and how to print the value of a variable in Python
.
The following formula is used to calculate simple interest:
Simple Interest = (P * N * R)/100
Where,
P
is the principal amount.N
is the time period in years. If this value is provided in months, we need to divide that by 12 to get the value in years.R
is the rate of interest.
The program will ask the user to enter these values. The following program shows how it works:
Approach 1: Python program to calculate simple interest:
#1
P = float(input("Enter the principal amount: "))
N = float(input("Enter the number of years: "))
R = float(input("Enter the rate of interest: "))
#2
SI = (P * N * R)/100
print("Simple interest : {}".format(SI))
Download it on GitHub
Explanation:
The commented numbers in the above program denote the step numbers below:
- Take the user input for the principal amount, time of interest in years, and the rate of interest. We are reading the inputs using the
input()
method and the values are converted to floating point numbers with thefloat()
method. Theinput()
method reads the user input as astring
. So, we need to convert it to afloat
.
- The principal amount, number of years, and the rate of interest values are assigned to the variables
P
,N
, andR
respectively.
- Calculate the simple interest using the same formula we discussed before. Print out the calculated value at the end of the program. We are using the
format()
method to print the result.
Sample Output:
If you run the above program, it will print output as below:
Approach 2: How to find the simple interest with a separate function:
We can also use a separate function to find the simple interest. The function will take these values as its parameters, calculate the simple interest and return it. The main
method will print the calculated simple interest value.
def get_simple_interest(p, n, r):
return (P * N * R) / 100
if __name__ == "__main__":
P = float(input("Enter the principal amount: "))
N = float(input("Enter the number of years: "))
R = float(input("Enter the rate of interest: "))
SI = get_simple_interest(P, N, R)
print("Simple interest : {}".format(SI))
Download it on GitHub
In this example, we created a new function get_simple_interest
to calculate the simple interest. It takes the principal amount, number of years, and the rate of interest as its parameters and returns the calculated simple interest. The main
function calls this function to calculate the simple interest value. This value is assigned to the variable SI
.
If you run this program, it will print similar results.
Conclusion:
In this tutorial, we have learned how to find out simple interest using user-provided values. Try to run the example above and raise a pull request on GitHub if you want to add anything.