How to find the Minimum of Three Numbers in Swift


How to find the Minimum of Three Numbers in Swift ?

Answer

To find the minimum of three numbers in Swift, you can use the built-in min function.



✐ Examples

1 Find Minimum of Three Integer Numbers

In this example,

  1. We create three integer variables named num1, num2, and num3 with specific values.
  2. We use the min function to find the minimum of num1 and num2, and store the result in a temporary variable.
  3. We then use the min function again to find the minimum of the temporary variable and num3.
  4. The min function returns the smallest of the three values.
  5. Finally, we print the minimum value to standard output.

Swift Program

import Foundation

let num1 = 5
let num2 = 10
let num3 = 3
let minOfFirstTwo = min(num1, num2)
let minimum = min(minOfFirstTwo, num3)
print("The minimum of \(num1), \(num2), and \(num3) is: \(minimum)")

Output

The minimum of 5, 10, and 3 is: 3

2 Find Minimum of Three Floating-Point Numbers

In this example,

  1. We create three floating-point variables named num1, num2, and num3 with specific values.
  2. We use the min function to find the minimum of num1 and num2, and store the result in a temporary variable.
  3. We then use the min function again to find the minimum of the temporary variable and num3.
  4. The min function returns the smallest of the three values.
  5. Finally, we print the minimum value to standard output.

Swift Program

import Foundation

let num1 = 15.5
let num2 = 10.3
let num3 = 12.8
let minOfFirstTwo = min(num1, num2)
let minimum = min(minOfFirstTwo, num3)
print("The minimum of \(num1), \(num2), and \(num3) is: \(minimum)")

Output

The minimum of 15.5, 10.3, and 12.8 is: 10.3

Summary

In this tutorial, we learned How to find the Minimum of Three Numbers in Swift language with well detailed examples.




More Swift Comparison Operations Tutorials

  1. How to find the Maximum of Two Numbers in Swift ?
  2. How to find the Minimum of Two Numbers in Swift ?
  3. How to find the Maximum of Three Numbers in Swift ?
  4. How to find the Minimum of Three Numbers in Swift ?