Swift Program to find the EVEN numbers from the array


In this article, we will learn how to write a swift program to find even numbers from the array.

Even number are those number which are completely divisible by 2. For example, 2, 6, 50, 20, etc. Here we use the following methods to find the even numbers from the array.

  • Using for-in loop

  • Using filter() function

Method 1: Using for-in loop

To find the even numbers from the given array we use for-in loop. Using for-in loop we iterate through each element of the given array and check if the element is even number or not.

Algorithm

  • Step 1 − Create a function.

  • Step 2 − Run for loop to iterate through each element go the array.

  • Step 3 − Check if the element is even or not using modulo operator. If remainder of the current element is 0, then the element is even. So print that element. Otherwise move to next element of the array.

  • Step 4 − Create an array of integer type.

  • Step 5 − Call the function and pass the array as a parameter.

Example

Following Swift program to find even numbers from the array.

import Foundation
import Glibc

// Function to find even number from the given array
func findEven(arr:[Int]) {
   for ele in arr {
    
      // Checking if the current element is even
      if ele % 2 == 0 {
         print(ele)
      }
   }
}
var numbers = [2, 3, 63, 98, 10, 31, 42, 8]
print("Original array:", numbers)
print("Even numbers are:")
findEven(arr:numbers)

Output

Original array: [2, 3, 63, 98, 10, 31, 42, 8]
Even numbers are:
2
98
10
42
8

Here in the above code, we have an array of integer type. Now we create a function to find the even numbers from the given array. So in this function, we use for loop to iterate through each element of the array and check if the number is even or not using modulo operator(%). The modulo operator return the remainder of the division operation. So if the remainder of the current element is 0 by dividing 2, that means the number is even number otherwise not.

Method 2: Using filter(_:) function

We can also use filter() function to find the even numbers from the given array. The filter(_:) function returns an array, which contains all the elements that satisfy the given condition.

Syntax

func filter(_mClosure:(self.Element) throws -> Bool)rethrows->[Self.Element]

Here, mClosure is a closure which which takes items from the given sequence as an argument and return a boolean value indicating whether the element should be included in the returned array or not.

Algorithm

  • Step 1 − Create an array of string type.

  • Step 2 − Print the original array.

  • Step 3 − Find even numbers from the given array using filter() function var result = ArrNum.filter{$0 % 2 == 0}

  • Step 4 − Print the output.

Example

Following Swift program to find even numbers from the array.

import Foundation
import Glibc
 
// Creating an array of integer type
var ArrNum = [34, 6, 21, 31, 8, 42, 9]

// Finding even numbers from the given array
var result = ArrNum.filter{$0 % 2 == 0}

print("Original Array: ", ArrNum)
print("Resultant Array: ", result)

Output

Original Array:  [34, 6, 21, 31, 8, 42, 9]
Resultant Array:  [34, 6, 8, 42]

Here in the above code, we have an array of integer type. Now we use filter() function to find even numbers from the given array. So for that we pass $0 % 2 == 0 as a closure in the filter() function. This closure takes elements from the given array starting from index 0 to end and find the remainder of each number by dividing 2. If the remainder is equal to 0 then that means the number is even number, so that number will add in the resultant array. This process will continue till the last element of the given array.

Conclusion

So this is how we can find even numbers from the array. Here both the methods return accurate result and they only works for integer data types.

Updated on: 17-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements