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

Getting odd #s & multiple of 7's

I think I know the codes, but not sure how to get odd numbers or multiple of 7's.

Challenge Task 1 of 1

For this challenge, we'd like to know in a range of values from 1 to 100, how many numbers are both odd, and a multiple of 7.

To start us off, I've written a for loop to iterate over the desired range of values and named the local constant n. Your job is to write an if statement inside the for loop to carry out the desired checks.

If the number is indeed both an odd number and a multiple of 7, append the value to the results array provided.

Hint: To check for an odd number you can either use the remainder operator and 3, or use the not operator to check for "not even"

/Users/mattconway/Desktop/Screen Shot 2016-01-14 at 11.32.08 AM.png

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

for n in 1...100
    // Enter your code below
    if n = [odd] && n = [multiple 7] {
    print (n)
    // End code 
}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Matt:

In this challenge we can use the remainder %, to check for the odd and the multiples of 7.

var results: [Int] = []

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

The for in loop goes through all the values in the range from 1 through 100, and checks first if it's an odd number, then it checks if its a multiple of 7. Why and how?

Remainder Operator

The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder).

Here’s how the remainder operator works. To calculate 9 % 4, you first work out how many 4s will fit inside 9. You can fit two 4s inside 9, and the remainder is 1

So to figure which numbers are odd we use the number 2

1 % 2 = 1 // Odd 
2 % 2 = 0 // Even number Because 2 fits in 2 once with no remainder.
3 % 2 = 1 // Odd Because 2 will fit one time in 3, but will have 1 remainder.
4 % 2 = 0 // Even number Because 2 fits in 4 twice with no remainder.
5 % 2 = 1 // Odd Because 2 will fit 2 times in 5, but will have 1 remainder.

and the same applies to the multiples of 7

7 % 7 = 0 // 7 is a multiple of 7 Because 7 fits in 7 with no remainder
14 % 7 = 0 // 14 is a multiple of 7 Because 7 fits in 14 two times with no remainder
21 % 7 = 0
28 % 7 = 0

I hope you find this explanation somewhat helpful.

Good luck

This is a very awesome thorough answer! Thanks!

Whoa! This answer helped me out a lot too, thanks!