# Swift Program to Calculate simple interest and compound interest

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

## Compound Interest

The interest applied on the principal and interest together over a certain period of time is known as the compound interest. Or in other words compound interest is known as the interest on interest. It is calculated after calculating amount.

### Formula

Following is the formula of compound interest −

Amount(A) = Principal(P)(1 + Rate/100)Time(t)C.I = Amount(A) - Principal(P)

Where

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

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

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

Amount(A) − Amount represent the total returned money that including principal and interest amount.

## 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 applying on 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 which is initially invested.

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

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

## Algorithm to calculate Simple interest and Compound interest

• Step 1 − Define 3 variables – Principal, Rate and Duration

• Step 2 − Assign value to those three variables

• Step 3 − Define a function for Simple interest and Compound interest

• Step 4 − Run the values through the S.I and C.I functions

• Step 5 − Display the final result on the output screen

## Example 1

The following program shows how to calculate simple interest and compound interest.

import Foundation
import Glibc

func simpleInterest(Principal: Double, Rate: Double, Time: Double) -> Double{
let Result1 = Principal * Time * Rate / 100
return Result1
}

func compoundInterest(Principal: Double, Rate: Double, Time: Double) -> Double{
let amount = Principal * pow((1 + Rate/100), Time)
let Result2 = amount - Principal
return Result2
}

print("Simple Interest is - ", simpleInterest(Principal:10000, Rate: 5, Time:5))
print("Compound Interest is - ", compoundInterest(Principal:10000, Rate: 5, Time:5))


### Output

Simple Interest is - 2500.0
Compound Interest is - 2762.815625000003


In the above c ode, we create two functions named as simpleInterest() and compoundInterest(). simpleInterest() function returns the simple interest of the given data(that is principal, rate, time) using the mathematical formula whereas compoundInterest() function returns the compound interest of the given data(that is principal, rate, time) using the mathematical formula. Now display the simple interest and compound interest by calling the simpleInterest() and compoundInterest() functions along with the values of the thre e

parameters named Principal:10000, Rate: 5, and Time:5

## Example 2

The following program shows how to calculate simple interest and compound interest by taking user inputs.

import Foundation
import Glibc

print(Principal)
print(Rate)
print(Duration)
func simpleInterest(P: Double, R: Double, T: Double) -> Double{
let Result1 = P * T * R / 100
return Result1
}

func compoundInterest(P: Double, R: Double, T: Double) -> Double{
let A = P * pow((1 + R/100), T)
let Result2 = A - P
return Result2
}

print("Press 1 for simple interest\n Press 2 for Compound Interest")

switch choice{
case 1:
print("Simple Interest is - ", simpleInterest(P: Principal, R: Rate, T: Duration))

case 2:
print("Compound Interest is - ", compoundInterest(P: Principal, R: Rate, T: Duration))

default:
print("Not a valid choice")
}


### STDIN Input

Please enter principal-
10000
3
6
Press 1 for simple interest
Press 2 for Compound Interest
2


## Output

Compound Interest is - 1940.5229652900016


In the above code, we first take the value of principal , rate, and time from the user. After that we create two functions named as simpleInterest() and compoundInterest(). As the name suggest the simpleInterest() function returns the simple interest and compoundInterest() function returns the compound interest of the input values. Now we ask the user if they want compound interest then press 1 or if they want simple interest then press 2. Here the user press 2 so the control of the program goes into the switch case statement and display the compound interest that is 1940.5229652900016.