Swift Program to Calculate Simple Interest


This tutorial will discuss how to write a swift program to calculate simple interest.

Simple interest is the most commonly used method to find the interest on the money for a given time period. In this method, the interest is always applied to the original principal amount with same the interest rate for every time period. It is calculated by multiplying the interest rate by the principal amount and the time.

Formula

Following is the formula of simple interest −

S.I = Principal Amount(P) x Time(T) x Rate(R)/100

Where

Principal(P) − Principal amount represents the amount that is initially invested.

Rate(R) − Rate represents the interest rate which applied to the principal amount for the given time period.

Time(T) − Time represents the time period for which the amount is invested in years.

Algorithm to Calculate Simple Interest

  • Step 1 − Define three variables (Principal, Rate and Time)

  • Step 2 − Assign the value of those variables

  • Step 3 − Implement Simple interest formula (Principal Amount(P) x Time(T) x Rate(R)/100)

  • Step 4 − Print the output

Example

The following program shows how to calculate Simple Interest.

import Foundation import Glibc var principal = 20000 var rate = 3 var time = 5 var SimpleInterest = (principal * time * rate)/100 print("Principal Amount is - ", principal,"rupees") print("Rate of the interest is - ", rate,"%") print("Time period is -", time, "Years") print("\nFinal simple interest is - ", SimpleInterest,"rupees")

Output

Principal Amount is - 20000 rupees 
Rate of the interest is - 3 % 
Time period is - 5 Years
Final simple interest is - 3000 rupees

In the above code, we calculate simple interest using mathematical formula as shown in the below code −

var SimpleInterest = (principal * time * rate)/100

Here, the principal, rate, and time period are 20000, 3, and 5 so the simple interest is 3000.

Example

The following program shows how to calculate Simple Interest with user-defined input.

import Foundation import Glibc print("Please enter the principal amount") var principal = Int(readLine()!)! print("Please enter the interest rate") var rate = Int(readLine()!)! print("Please enter the time period") var time = Int(readLine()!)! var SimpleInterest = (principal * time * rate)/100 print("\nEntered Principal Amount is - ", principal,"rupees") print("Entered Interest Rate is - ", rate,"%") print("Entered Time period is -", time,"Years") print("\nFinal simple interest is - ", SimpleInterest,"rupees")

STDIN Input

Please enter the principal amount
10000 
Please enter the interest rate 
4 
Please enter the time period 
5

Output

Entered Principal Amount is -  10000 rupees
Entered Interest Rate is -  4 %
Entered Time period is - 5 Years

Final simple interest is -  2000 rupees

In the above code, we calculate simple interest using mathematical formula as shown in the below code −

var SimpleInterest = (principal * time * rate)/100

Here the principal, rate, and time period are taken from the user at runtime using the readLine() function. So the simple interest of the given data is 3000.

Updated on: 04-Aug-2022

777 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements