How to know if a number is odd or even in Swift?

Asif Bilal picture Asif Bilal · Jun 5, 2014 · Viewed 69.7k times · Source

I have an array of numbers typed Int.

I want to loop through this array and determine if each number is odd or even.

How can I determine if a number is odd or even in Swift?

Answer

Charith Nidarsha picture Charith Nidarsha · Jun 5, 2014
var myArray = [23, 54, 51, 98, 54, 23, 32];
for myInt: Int in myArray{
  if myInt % 2 == 0 {
    println("\(myInt) is even number")
  } else {
    println("\(myInt) is odd number")
  }
}