# Swift Program to Get Odd and Even Numbers From the Array

SwiftServer Side ProgrammingProgramming

#### Swift 4 Online Training

38 Lectures 1 hours

#### Swift Basics: Learn to Code from Scratch [For Beginners]

13 Lectures 2 hours

#### How to create Tic-Tac-Toe Game for iPhone using Swift in Xcode - iOS App

7 Lectures 1 hours

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

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. So to get odd and even from the given integer array we uses for loop and if statement. As we know that even number are completely divisible by 2 whereas odd numbers are not. So we find the remainder using % operator and the check if the remainder is equal to zero then the number is even number. Otherwise the number is odd number.

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]


## Algorithm

Following is the algorithm −

• Step 1 − Declare and initialise an array of Integer type

• Step 2 − Find the size of the array using count property and store the result in a variable −

var size = myArray.count

• Step 3 − Declare two arrays of Integer types named evenArray and oddArray.

• Step 4 − Find the even elements of the array −

for j in 0..< size {
if (myArray[j] % 2 == 0){
evenArray.append(myArray[j])
}
}

• Step 5 − Find the odd elements of the array −

// Finding odd elements
for k in 0..< size {
if(myArray[k] % 2 != 0){
oddArray.append(myArray[k])
}
}

• Step 6 − Print the output

## Example

The following program shows how to get odd and even numbers from the array.

import Foundation
import Glibc

// Creating an array of Integer Type
var myArray = [23, 5, 6, 7, 89, 64, 2, 19]
var size = myArray.count
print("Array:", myArray)
var evenArray = [Int]()
var oddArray = [Int]()

// Finding even elements
for j in 0..<size{
if (myArray[j] % 2 == 0){
evenArray.append(myArray[j])
}
}

// Finding odd elements
for k in 0..<size{
if (myArray[k] % 2 != 0){
oddArray.append(myArray[k])
}
}

print("Even elements:", evenArray)
print("Odd elements:", oddArray)


### Output

Array: [23, 5, 6, 7, 89, 64, 2, 19]
Even elements: [6, 64, 2]
Odd elements: [23, 5, 7, 89, 19]


Here, in the above code, we have an array of integer type. Now we segregate the odd and even elements from the array and then store them into different arrays using append() function. For the even numbers we use the following code −

// Loop runs till the length of the array
for j in 0..<size{

// Find the remainder and check if remainder is equal to 0
if (myArray[j] % 2 == 0){

// Here if the remainder is 0 then add the element to the evenArray. Otherwise check for the next element.
evenArray.append(myArray[j])
}
}


For the odd numbers we use the following code −

// Loop runs till the length of the array
for k in 0..<size{

// Find the remainder and check if remainder is not equal to 0
if (myArray[k] % 2 != 0){

// Here if the remainder is not equal to 0 then add the element to the oddArray. Otherwise check for the next element.
oddArray.append(myArray[k])
}
}

Updated on 11-Oct-2022 12:11:43