Swift Program to Get Odd and Even Numbers From the Array Using Library Function

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to get odd and even numbers from the array using library function.

Odd numbers are those number which are not completely divisible by 2, or in other words when a number divided by 2 and leave some remainder then such type of numbers are known as odd numbers. Following is the list of some odd numbers −

1, 3, 5, 7, 11, 13, …..

Even numbers are those numbers which are completely divisible by 2, or in other words when a number is divided by 2 and leaves 0 remainder the such type of numbers are known as even numbers. Following is the list of some even numbers −

2, 4, 6, 8, 10, 12, 14, ……..

An array is an ordered collection which is used to store same type of data. In an array, Swift provide an in-built function named as filter(). This function returns all the items from the array that satisfies the given condition. So we use filter() to filter out odd and even numbers from the given integer array.

Below is a demonstration of the same −

Suppose our given input is −

MyArray = [23, 5, 6, 7, 89, 64, 2, 19]

The desired output would be −

Odd Numbers = [6, 64, 2]
Even Numbers = [23, 5, 7, 89, 19]

Syntax

Following is the syntax −

Output = myArr.filter({condition})

Below is a demonstration of the same −

Algorithm

Following is the algorithm −

  • Step 1 − Create an array of integer type.

  • Step 2 − Finding the odd and even numbers from the array using filter() function and store the result into res variable −

For odd numbers −

var minElement = myarray.min()!

For even numbers −

var res = myNumber.filter({$0 % 2 == 0})

Note − Here $0 is the shorthand representation of first argument.

  • Step 3 − Print the output

Example 1

The following program shows how to get odd numbers from the array using library function.

import Foundation import Glibc // Creating an array of Integer Type var myNumber = [23, 5, 6, 7, 89, 64, 2, 19] print("Original array: ", myNumber) // Finding the odd numbers from the array var res = myNumber.filter({$0 % 2 != 0}) print("Odd Elements:", res)

Output

Original array: [23, 5, 6, 7, 89, 64, 2, 19]
Odd Elements: [23, 5, 7, 89, 19]

Here, in the above code, we have an array of integer type = [23, 5, 6, 7, 89, 64, 2, 19]. Now we find odd numbers from the array using the filter() function, In this function, we pass a condition that is {$0 % 2 != 0}, this condition will check the remainder is not equal to 0. If the remainder is not equal to 0(condition is true), then this function will return the number. Otherwise it will move to next number to check for odd number. So the working of the above code is −

var res = myNumber.filter({$0 % 2 != 0}) = myNumber.filter({23 % 2 != 0}) = myNumber.filter({1 != 0}) = condition is true so return 23
var res = myNumber.filter({5 % 2 != 0}) = myNumber.filter({1 != 0}) = condition is true so return 5
var res = myNumber.filter({6 % 2 != 0}) = myNumber.filter({0 != 0}) = condition is false return nothing

… so on till the last element.

So the output is [23, 5, 7, 89, 19].

Example 2

The following program shows how to get even numbers from the array using library function.

import Foundation import Glibc // Creating an array of Integer Type var myNumber = [23, 5, 6, 7, 89, 64, 2, 19] print("Original array: ", myNumber) // Finding the even numbers from the array var res = myNumber.filter({$0 % 2 == 0}) print("Even Elements:", res)

Output

Original array: [23, 5, 6, 7, 89, 64, 2, 19]
Even Elements: [6, 64, 2]

Here, in the above code, we have an array of integer type = [23, 5, 6, 7, 89, 64, 2, 19]. Now we find even numbers from the array using the filter() function, In this function, we pass a condition that is {$0 % 2 == 0}, this condition will check the remainder is equal to 0. If the remainder is equal to 0(condition is true), then this function will return the number. Otherwise it will move to next number to check for odd number. So the working of the above code is;

var res = myNumber.filter({$0 % 2 != 0}) = myNumber.filter({23 % 2 != 0}) = myNumber.filter({1 != 0}) = condition is false so return nothing
var res = myNumber.filter({5 % 2 != 0}) = myNumber.filter({1 != 0}) = condition is false so return nothing
var res = myNumber.filter({6 % 2 != 0}) = myNumber.filter({0 != 0}) = condition is true return 6

… so on till the last element.

So the output is [6, 64, 2].

raja
Updated on 11-Oct-2022 12:08:56

Advertisements