Swift

func even(number: Int) -> Bool {
*// Return true if number is evenly divisible by 2.*
return number % 2 == 0
}
*// Test the parity of these numbers.*
**for** i in *0...5* {
let result = even(i)
*// Display result.*
print("\(i) = \(result)")
}0 = true
1 = false
2 = true
3 = false
4 = true
5 = false

func odd(number: Int) -> Bool {
*// Divide number by 2.*
*// ... If remainder is 1, we have a positive odd number.*
*// ... If remainder is -1, it is odd and negative.*
*// ... Same as "not even."*
return number % 2 != 0
}
**for** i in *-3...3* {
let result = odd(i)
print("\(i) = \(result)")
}-3 = true
-2 = false
-1 = true
0 = false
1 = true
2 = false
3 = true

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

No updates found for this page.