w3resource

Swift Array Programming Exercises: Test if an array of integers does not contain a 3 or a 5

Swift Array Programming: Exercise-12 with Solution

Write a Swift program to test if an array of integers does not contain a 3 or a 5.

Pictorial Presentation:

Swift Array Programming Exercises: Test if an array of integers does not contain a 3 or a 5

Sample Solution:

Swift Code:

func no35(_ a: [Int]) -> Bool {
    if a.contains(3) || a.contains(5)
    {
        return false
    } 
    else
    {
        return true
    }
}
print(no35([2, 5]))
print(no35([2, 3, 5]))
print(no35([2, 4, 7]))

Sample Output:

false
false
true

Swift Programming Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Swift program to test if an array of integers contains a 3 or a 5.
Next: Write a Swift program to create a new array with double the lenght of a given array of integers and its last element is the same as the given array. The given array will be length 1 or more. By default, a new integer array contains all 0's.

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/array/swift-array-exercise-12.php