# Swift Program to Calculate Compound Interest

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

The interest applied to the principal and interest together over a certain period of time is known as compound interest. In other words, the compound interest is known as the interest on interest. It is calculated after calculating the 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 that is initially invested.

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

Amount(A) − Amount represents the total returned the money that includes principal and interest amount.

## Algorithm to Calculate Compound Interest

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

• Step 2 − Assign the value of those variables

• Step 3 − Implement Simple interest formula (Amount(A) = Principal(P)(1 + Rate/100)Time(t))

• Step 4 − Print the output

## Example 1

The following program shows how to calculate the compound interest.

import Foundation
import Glibc

var Principal = 80000.0
var Rate = 4.0
var Time = 6.0

var Amount = Principal * pow((1 + Rate/100), Time)

var CI = Amount - Principal

print("Principal is-", Principal)
print("Interest rate is-", Rate,"%")
print("Time interval is-", Time,"Years")
print("Amount is-", Amount)
print("\nFinal compound interest is", CI)


## Output

Principal is- 80000.0
Interest rate is- 4.0 %
Time interval is- 6.0 Years
Amount is- 101225.52147968003

Final compound interest is 21225.52147968003


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

var Amount = Principal * pow((1 + Rate/100), Time)
var CI = Amount - Principal


Here, we first calculate the amount and using the amount we calculate the compound interest. The principal, rate, and time period are 80000, 4, and 6 so the amount is 101225.52147968003 and the compound interest is 21225.52147968003.

## Example 2

The following program shows how to calculate the compound interest with user input.

import Foundation
import Glibc

print("Please enter the rate of interest-")

var Amount = Principal * pow((1 + Rate/100), TimeInterval)

var CI = Amount - Principal

print("Entered Principal is-", Principal)
print("Entered Interest rate is-", Rate)
print("Entered Time interval is-", TimeInterval)
print("--------------")
print("Amount is-", Amount)
print("Compound interest is-", CI)


### STDIN Input

Please enter the principal amount-
10000
Please enter the rate of interest-
3
1


## Output

Entered Principal is- 10000.0
Entered Interest rate is- 3.0
Entered Time interval is- 1.0
--------------
Amount is- 10300.0
Compound interest is- 300.0


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

var Amount = Principal * pow((1 + Rate/100), TimeInterval)
var CI = Amount - Principal


Here first we calculate the amount and then we calculate compound interest. The principal, rate, and time period are taken from the user using the readLine() function. So the compound interest of the given data is 300.

Updated on: 01-Aug-2022

491 Views 