How to find the Maximum of Three Numbers in Swift - Step by Step Examples
How to find the Maximum of Three Numbers in Swift ?
Answer
To find the maximum of three numbers in Swift, you can use the built-in max
function.
✐ Examples
1 Find Maximum of Three Integer Numbers
In this example,
- We create three integer variables named
num1
,num2
, andnum3
with specific values. - We use the
max
function to find the maximum ofnum1
andnum2
, and store the result in a temporary variable. - We then use the
max
function again to find the maximum of the temporary variable andnum3
. - The
max
function returns the largest of the three values. - Finally, we print the maximum value to standard output.
Swift Program
import Foundation
let num1 = 5
let num2 = 10
let num3 = 3
let maxOfFirstTwo = max(num1, num2)
let maximum = max(maxOfFirstTwo, num3)
print("The maximum of \(num1), \(num2), and \(num3) is: \(maximum)")
Output
The maximum of 5, 10, and 3 is: 10
2 Find Maximum of Three Floating-Point Numbers
In this example,
- We create three floating-point variables named
num1
,num2
, andnum3
with specific values. - We use the
max
function to find the maximum ofnum1
andnum2
, and store the result in a temporary variable. - We then use the
max
function again to find the maximum of the temporary variable andnum3
. - The
max
function returns the largest of the three values. - Finally, we print the maximum value to standard output.
Swift Program
import Foundation
let num1 = 15.5
let num2 = 10.3
let num3 = 12.8
let maxOfFirstTwo = max(num1, num2)
let maximum = max(maxOfFirstTwo, num3)
print("The maximum of \(num1), \(num2), and \(num3) is: \(maximum)")
Output
The maximum of 15.5, 10.3, and 12.8 is: 15.5
Summary
In this tutorial, we learned How to find the Maximum of Three Numbers in Swift language with well detailed examples.