Swift programming
odd and even numbers
Swift tutorial
coding basics
Swift development

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Checking whether a number is odd or even in Swift is simple, but there are two slightly different styles worth knowing. The classic approach uses the remainder operator %, and the more readable modern approach often uses isMultiple(of:) on integer types.

Use the Remainder Operator

An integer is even if dividing by 2 leaves remainder 0. It is odd if the remainder is not 0.

swift
1import Foundation
2
3func isEven(_ value: Int) -> Bool {
4    value % 2 == 0
5}
6
7func isOdd(_ value: Int) -> Bool {
8    value % 2 != 0
9}
10
11print(isEven(4))
12print(isOdd(5))

This is the most familiar solution and works well in almost every Swift codebase.

Use isMultiple(of:) for Readability

Swift integer types also support isMultiple(of:), which can read more clearly than remainder logic.

swift
1import Foundation
2
3let number = 12
4
5if number.isMultiple(of: 2) {
6    print("even")
7} else {
8    print("odd")
9}

This is often easier to read in business logic because the intent is explicit.

Negative Numbers Still Work

Parity checks work the same way for negative integers.

swift
print((-4).isMultiple(of: 2))
print((-3) % 2 == 0)

-4 is still even and -3 is still odd. You do not need special-case logic just because the number is negative.

Wrap the Check in a Small Helper

If the parity check appears often in a project, a tiny extension can make call sites cleaner.

swift
1import Foundation
2
3extension BinaryInteger {
4    var isEven: Bool { isMultiple(of: 2) }
5    var isOdd: Bool { !isEven }
6}
7
8print(7.isOdd)
9print(10.isEven)

This works nicely because it applies to integer types more generally, not just Int.

Parity checks also show up often inside loops and alternating UI logic. Keeping the check as a tiny helper or computed property makes those call sites easier to read than repeating % 2 conditions throughout the codebase.

Use the Right Numeric Type

Odd and even are integer concepts. If your value is a floating-point number, you should first decide whether it is supposed to represent an integer at all.

swift
1let value = 6.0
2
3if value.truncatingRemainder(dividingBy: 2) == 0 {
4    print("even as a whole-number value")
5}

That kind of check is usually a signal that the model should perhaps use an integer type instead.

A Small Real-World Example

Parity checks often appear in UI and collection logic. For example, alternating row styling or filtering only even values can stay very readable when the intent is named clearly.

swift
let values = Array(1...10)
let evens = values.filter { $0.isMultiple(of: 2) }
print(evens)

This kind of example is simple, but it shows why a readable parity check is useful beyond toy exercises.

Common Pitfalls

A common mistake is using % with floating-point values and assuming the same semantics as integers. If parity matters, integer types are usually the right representation.

Another is writing more complicated branching than necessary. A parity check should stay tiny and direct.

Developers also sometimes forget that negative numbers still follow the same odd-even rules, so there is no need for extra sign handling.

Summary

  • Use % 2 == 0 to test whether an integer is even.
  • Use % 2 != 0 to test whether an integer is odd.
  • 'isMultiple(of: 2) is often the clearest readable form in Swift.'
  • Negative integers follow the same parity rules as positive integers.
  • If parity matters often, a small integer extension can keep the code cleaner.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions