- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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]) } }

- Related Questions & Answers
- Java program to Print Odd and Even Number from an Array
- Even numbers at even index and odd numbers at odd index in C++
- Java program to print odd and even number from a Java array.
- How to find the Odd and Even numbers in an Array in java?
- Python program to Count Even and Odd numbers in a List
- How to get odd and even position characters from a string?
- C program to store even, odd and prime numbers into separate files
- Write a Golang program to find odd and even numbers using bit operation
- Largest Even and Odd N-digit numbers in C++
- Count Odd and Even numbers in a range from L to R in C++
- Adding only odd or even numbers JavaScript
- Program to sort all even and odd numbers in increasing and decreasing order respectively in Python
- C++ program for the Array Index with same count of even or odd numbers on both sides?
- Missing even and odd elements from the given arrays in C++
- C# program to split the Even and Odd integers into different arrays
- Odd even sort in an array - JavaScript