Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Collections and Control Flow Control Flow With Conditional Statements Working With Logical Operators

How do you correctly check for the odd number?

Everything else seems fine but I'm unsure on how to write the code to check for an odd number.

3 Answers

Matthew Long
Matthew Long
28,407 Points

An odd number can be checked by seeing if a numbers remainder is not equal to 0 when dividing by 2.

let oddNumber = numberToCheck % 2 != 0

The above will evaluate to true or false.

Hope this helps you pass the challenge! :smile:

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Cass;

I think the easiest method is to use the Remainder Operator, %. You should be able to do something along the lines of:

if your_number % 2 == 0 {
    // even number handling
} else {
   // odd number handling
}

That will check to see if your_number is evenly divisible by two and, thus, is an even number.

Post back if you are still stuck or have further questions.

Happy coding,
Ken

thank yous!