In this tutorial, we will learn how to calculate simple interest in Swift. We will understand the formula for simple interest, write an algorithm, and implement it step by step while addressing edge cases, followed by testing the implementation with examples.


What is Simple Interest?

Simple Interest (SI) is calculated using the formula:

SI = (Principal × Rate × Time) / 100

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 simple interest is:

SI = (1000 × 5 × 2) / 100 = 100

Algorithm to Calculate Simple Interest

  1. Take the Principal, Rate, and Time as inputs.
  2. Validate the inputs:
    1. Ensure the values for Principal, Rate, and Time are non-negative.
  3. Apply the formula: SI = (Principal × Rate × Time) / 100.
  4. Return the calculated simple interest.

Next, we will implement this algorithm step by step in Swift, starting with base cases and input validation.


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:

</>
Copy
func calculateSimpleInterest(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 Simple Interest Formula

Once the inputs are validated, apply the formula to calculate the simple interest:

</>
Copy
func calculateSimpleInterest(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 simpleInterest = (principal * rate * time) / 100
    return simpleInterest
}

Explanation:

if principal < 0 || rate < 0 || time < 0: Validates that all inputs are non-negative.

let simpleInterest = (principal * rate * time) / 100: Applies the simple interest formula.

return simpleInterest: Returns the calculated value.

3. Test the Function

Let’s test the function with some example inputs:

</>
Copy
// Test cases
if let si1 = calculateSimpleInterest(principal: 1000, rate: 5, time: 2) {
    print("Given: Principal = 1000, Rate = 5%, Time = 2 years")
    print("Calculated Simple Interest: \(si1)\n") // Output: 100
}

if let si2 = calculateSimpleInterest(principal: 1500, rate: 4.5, time: 3) {
    print("Given: Principal = 1500, Rate = 4.5%, Time = 3 years")
    print("Calculated Simple Interest: \(si2)\n") // Output: 202.5
}

if let si3 = calculateSimpleInterest(principal: -1000, rate: 5, time: 2) {
    print("Given: Principal = -1000, Rate = 5%, Time = 2 years")
    print("Calculated Simple Interest: \(si3)\n")
} else {
    print("Given: Principal = -1000, Rate = 5%, Time = 2 years")
    print("Error: Inputs cannot be negative.\n") // Error Output
}

The function calculates the simple interest for valid inputs and handles negative inputs by displaying an error message.


Complete Swift Program

Here’s the complete Swift program to calculate simple interest for a give principal amount and interest rate:

main.swift

</>
Copy
import Foundation

// Function to calculate simple interest
func calculateSimpleInterest(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 simpleInterest = (principal * rate * time) / 100
    return simpleInterest
}

// Test cases
if let si1 = calculateSimpleInterest(principal: 1000, rate: 5, time: 2) {
    print("Simple Interest: \(si1)") // Output: 100
}

if let si2 = calculateSimpleInterest(principal: 1500, rate: 4.5, time: 3) {
    print("Simple Interest: \(si2)") // Output: 202.5
}

if let si3 = calculateSimpleInterest(principal: -1000, rate: 5, time: 2) {
    print("Simple Interest: \(si3)") // Output: Error
}

Output:

Given: Principal = 1000, Rate = 5%, Time = 2 years
Calculated Simple Interest: 100.0

Given: Principal = 1500, Rate = 4.5%, Time = 3 years
Calculated Simple Interest: 202.5

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

Swift Program to Calculate Simple Interest