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.
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.
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.
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.
-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.
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.
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.
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 == 0to test whether an integer is even. - Use
% 2 != 0to 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
- How to know when UITableView did scroll to bottom in iPhone?
- How to know XCTestExpectation current fulfillment count
- How to launch an Activity from another Application in Android
- How to launch an Activity from another Application in Android
- How to launch Safari and open URL from iOS app
- How to lay out Views in RelativeLayout programmatically?
- How to lazy load images in ListView in Android
- How to let the app know if it's running Unit tests in a pure Swift project?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseTrack 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.