In this tutorial, we will learn how to calculate compound interest in Swift. We will understand the formula for compound interest, write an algorithm to implement it, and cover edge cases to handle invalid inputs. Finally, we will test the implementation with sample inputs and display the results.
What is Compound Interest?
Compound Interest (CI) is calculated using the formula:
CI = Principal × (1 + Rate / 100)Time - Principal
Where:
Principal
: The initial amount of money lent or borrowed.Rate
: The annual interest rate (in percentage).Time
: The duration for which the interest is calculated (in years).
For example, if the principal is 1000
, the rate is 5%
, and the time is 2 years
, the compound interest is:
CI = 1000 × (1 + 5 / 100)2 - 1000 = 102.50
Algorithm to Calculate Compound Interest
- Take the
Principal
,Rate
, andTime
as inputs. - Validate the inputs:
- Ensure the values for
Principal
,Rate
, andTime
are non-negative.
- Ensure the values for
- Apply the formula:
CI = Principal × (1 + Rate / 100)Time - Principal
. - Return the calculated compound interest.
Step-by-Step Implementation in Swift
1. Handle Base Cases
We first validate that the inputs are non-negative. If any input is negative, the function will return an error message:
func calculateCompoundInterest(principal: Double, rate: Double, time: Double) -> Double? {
// Ensure inputs are non-negative
if principal < 0 || rate < 0 || time < 0 {
print("Error: Inputs cannot be negative.")
return nil
}
return 0 // Placeholder for further steps
}
2. Apply the Compound Interest Formula
Once the inputs are validated, apply the formula to calculate compound interest:
func calculateCompoundInterest(principal: Double, rate: Double, time: Double) -> Double? {
// Ensure inputs are non-negative
if principal < 0 || rate < 0 || time < 0 {
print("Error: Inputs cannot be negative.")
return nil
}
// Apply the formula
let amount = principal * pow(1 + rate / 100, time)
let compoundInterest = amount - principal
return compoundInterest
}
Explanation:
if principal < 0 || rate < 0 || time < 0
: Ensures inputs are non-negative.
let amount = principal * pow(1 + rate / 100, time)
: Calculates the total amount after compound interest.
let compoundInterest = amount - principal
: Computes the compound interest by subtracting the principal from the total amount.
3. Test the Function
Let’s test the function with some example inputs:
// Test cases
if let ci1 = calculateCompoundInterest(principal: 1000, rate: 5, time: 2) {
print("Given: Principal = 1000, Rate = 5%, Time = 2 years")
print("Calculated Compound Interest: \(ci1)\n") // Output: 102.5
}
if let ci2 = calculateCompoundInterest(principal: 1500, rate: 4.5, time: 3) {
print("Given: Principal = 1500, Rate = 4.5%, Time = 3 years")
print("Calculated Compound Interest: \(ci2)\n") // Output: 212.47
}
if let ci3 = calculateCompoundInterest(principal: -1000, rate: 5, time: 2) {
print("Given: Principal = -1000, Rate = 5%, Time = 2 years")
print("Calculated Compound Interest: \(ci3)\n")
} else {
print("Given: Principal = -1000, Rate = 5%, Time = 2 years")
print("Error: Inputs cannot be negative.\n") // Error Output
}
Complete Swift Program
Here’s the complete Swift program to calculate compound interest:
main.swift
import Foundation
// Function to calculate compound interest
func calculateCompoundInterest(principal: Double, rate: Double, time: Double) -> Double? {
// Ensure inputs are non-negative
if principal < 0 || rate < 0 || time < 0 {
print("Error: Inputs cannot be negative.")
return nil
}
// Apply the formula
let amount = principal * pow(1 + rate / 100, time)
let compoundInterest = amount - principal
return compoundInterest
}
// Test cases
if let ci1 = calculateCompoundInterest(principal: 1000, rate: 5, time: 2) {
print("Given: Principal = 1000, Rate = 5%, Time = 2 years")
print("Calculated Compound Interest: \(ci1)\n") // Output: 102.5
}
if let ci2 = calculateCompoundInterest(principal: 1500, rate: 4.5, time: 3) {
print("Given: Principal = 1500, Rate = 4.5%, Time = 3 years")
print("Calculated Compound Interest: \(ci2)\n") // Output: 212.47
}
if let ci3 = calculateCompoundInterest(principal: -1000, rate: 5, time: 2) {
print("Given: Principal = -1000, Rate = 5%, Time = 2 years")
print("Calculated Compound Interest: \(ci3)\n")
} else {
print("Given: Principal = -1000, Rate = 5%, Time = 2 years")
print("Error: Inputs cannot be negative.\n") // Error Output
}
Output:
Given: Principal = 1000, Rate = 5%, Time = 2 years
Calculated Compound Interest: 102.5
Given: Principal = 1500, Rate = 4.5%, Time = 3 years
Calculated Compound Interest: 211.7491874999996
Error: Inputs cannot be negative.
Given: Principal = -1000, Rate = 5%, Time = 2 years
Error: Inputs cannot be negative.
Program ended with exit code: 0
Screenshot from Xcode