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