w3resource

Swift Basic Programming Exercise: Find the largest number among three given integers

Swift Basic Programming: Exercise-14 with Solution

Write a Swift program to find the largest number among three given integers.

Pictorial Presentation:

Swift Basic Programming Exercise: Find the largest number among three given integers.

Sample Solution:

Swift Code:

func max_three(_ x: Int, _ y: Int, _ z: Int) -> Int {
    if x > y, x > z 
    {
        return x
    } 
    else if y > z, y > x 
    {
        return y
    } 
    else if z > y, z > x 
    {
        return z
    } 
    else if x == y, y > z 
    {
        return x
    }
    else if y == z, z > x 
    {
        return y
    } 
    else 
    {
        return x
    }
}

print(max_three(1, 2, 3))
print(max_three(3, 2, 1))
print(max_three(-3, -2, 0))

Sample Output:

3
3
0

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Swift program to check if a given string begins with "fix", except the 'f' can be any character or number.
Next: Write a Swift program that accept two integer values and to test which value is nearest to the value 10, or return 0 if both integers have same distance from 10.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/swift-programming-exercises/basic/swift-basic-exercise-14.php