How to find the Minimum of Two Numbers in Swift - Step by Step Examples



How to find the Minimum of Two Numbers in Swift ?

Answer

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



✐ Examples

1 Find Minimum of Two Integer Numbers

In this example,

  1. We create two integer variables named num1 and num2 with specific values.
  2. We use the min function to find the minimum of num1 and num2.
  3. The min function returns the smaller of the two values.
  4. Finally, we print the minimum value to standard output.

Swift Program

import Foundation

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

Output

The minimum of 5 and 10 is: 5

2 Find Minimum of Two Floating-Point Numbers

In this example,

  1. We create two floating-point variables named num1 and num2 with specific values.
  2. We use the min function to find the minimum of num1 and num2.
  3. The min function returns the smaller of the two values.
  4. Finally, we print the minimum value to standard output.

Swift Program

import Foundation

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

Output

The minimum of 15.5 and 10.3 is: 10.3

Summary

In this tutorial, we learned How to find the Minimum of Two 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 ?