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 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Logical Operators

Travinna Nerestant
Travinna Nerestant
1,609 Points

Remainder Operator and 3 for Odd Number

In this challenge, I have figured out how to use the not operator to check for "not even" ( n % 2 != 0). How do you use the remainder operator and 3 to find an odd number?

logicalOperators.swift
var results: [Int] = []

for n in 1...100 {
    // Enter your code below
    if (n % 3 == 0) && (n % 7 == 0) {
    results.append(n)
    }
    // End code 
}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello :

Well it's simple actually..

we know that odd numbers are numbers that when you use the remainder operator, will not equal 0.

For example :

6 % 2 = 0 //Even
7 % 2 = 1  //Odd
8 % 2 = 0 //Even
9 % 2 = 1 //Odd

So in this case you can do this ..

if n % 2 == 0 {
print("Even Number")
} else if n % 2 != 0 {
print("Odd Number")
}

Hope this guides you into a solution on your code :)

Caleb McGuire
Caleb McGuire
5,818 Points

Hmm - Are you sure that last part is right? 22 % 3 = 1

22 is an even number I believe.