Swift – Find the largest of three numbers

In this tutorial, we will learn how to find the largest of three numbers in Swift. We will explore different approaches, including using conditional statements and the built-in max function.


Using Conditional Statements

The simplest way to find the largest of three numbers is by using if and else statements to compare the numbers.

Here’s an example:

</>
Copy
func findLargest(of a: Int, _ b: Int, _ c: Int) -> Int {
    if a >= b && a >= c {
        return a
    } else if b >= a && b >= c {
        return b
    } else {
        return c
    }
}

// Example usage
let largest = findLargest(of: 10, 25, 15)
print("The largest number is \(largest).")

Explanation:

  • a >= b && a >= c: Checks if a is greater than or equal to both b and c.
  • b >= a && b >= c: Checks if b is greater than or equal to both a and c.
  • If neither of the above conditions is true, c is returned as the largest number.

Using the max Function

Swift provides a built-in max function that can be used to find the largest of two or more numbers. This approach is simpler and more concise.

Here’s how to use the max function:

</>
Copy
func findLargestUsingMax(of a: Int, _ b: Int, _ c: Int) -> Int {
    return max(a, b, c)
}

// Example usage
let largestUsingMax = findLargestUsingMax(of: 10, 25, 15)
print("The largest number using max() is \(largestUsingMax).")

Explanation:

  • max(a, b, c): Compares a, b, and c and returns the largest value.
  • This approach is more concise compared to using conditional statements.

Handling Edge Cases

Both approaches handle edge cases where two or more numbers are equal. For example:

</>
Copy
let largestEqualNumbers = findLargest(of: 20, 20, 15)
print("The largest number is \(largestEqualNumbers).") // Output: 20

let largestAllEqual = findLargest(of: 10, 10, 10)
print("The largest number is \(largestAllEqual).") // Output: 10

In these cases, the function will return the first occurrence of the largest value.


Complete Swift Program

Here’s the complete Swift program demonstrating both approaches:

</>
Copy
import Foundation

// Approach 1: Using conditional statements
func findLargest(of a: Int, _ b: Int, _ c: Int) -> Int {
    if a >= b && a >= c {
        return a
    } else if b >= a && b >= c {
        return b
    } else {
        return c
    }
}

// Approach 2: Using the max function
func findLargestUsingMax(of a: Int, _ b: Int, _ c: Int) -> Int {
    return max(a, b, c)
}

// Test cases
let largest = findLargest(of: 10, 25, 15)
print("The largest number is \(largest).")

let largestUsingMax = findLargestUsingMax(of: 10, 25, 15)
print("The largest number using max() is \(largestUsingMax).")

let largestEqualNumbers = findLargest(of: 20, 20, 15)
print("The largest number is \(largestEqualNumbers).")

let largestAllEqual = findLargest(of: 10, 10, 10)
print("The largest number is \(largestAllEqual).")

Output:

The largest number is 25.
The largest number using max() is 25.
The largest number is 20.
The largest number is 10.
Program ended with exit code: 0

This program provides two approaches to find the largest of three numbers, handling edge cases and providing clear output for all scenarios.

Swift Program to Find the largest of three numbers