w3resource

Swift Basic Programming Exercise: Accept two integer values and return true if one is negative and one is positive

Swift Basic Programming: Exercise-4 with Solution

Write a Swift program to accept two integer values and return true if one is negative and one is positive. Return true only if both are negative.

Pictorial Presentation:

Swift Basic Programming Exercise: Accept two integer values and return true if one is negative and one is positive.

Sample Solution:

Swift Code:

func test_num(x: Int, y: Int) -> Bool {
    if x > 0 && y < 0 
    {
        return true
    }
    else if x < 0 && y > 0 
    {
        return true
    } 
    else if x < 0 && y < 0 
    {
        return true
    } 
    else
    {
        return false
    }
}

print(test_num(x:12, y:-5))
print(test_num(x:-12, y:5))
print(test_num(x:-12, y:-5))
print(test_num(x:12, y:5))


Sample Output:

true
true
true
false

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Swift program that accept two integer values and return true if one of them is 20 or if their sum is 20.
Next: Write a Swift program to add "Is" to the front of a given string.

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts