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

Michael Bau
Michael Bau
3,394 Points

How to create logical statement deriving uneven numbers and multiples of 7?

Hi,

I am completely stuck here! I have no clue how to construct any logical statements that would derive uneven numbers or multiples of 7.

Could anyone assist with a hint?

Best regards, Michael

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

for n in 1...100 {
   // Enter your code below

   // End code 
}

2 Answers

Alexandra Barnett
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexandra Barnett
Front End Web Development Techdegree Graduate 46,473 Points

Hi Michael! So, to check for odd numbers or multiples, you need to use the modulus operator (%). The modulus operator gives you any remainder when two numbers are divided together. For example, 2 % 2 would equal 0 because there would be no remainder. Another example would be 11 %5 would equal 1 because 5 goes into 11 two times with 1 as the remainder. This means that, to check an odd number, we need to have a remainder when two numbers are divided together. e.g. 7 % 5 != 0. To check for multiples, a number is a multiple of 7, for example, if it is divisible by 7 with no remainder (7 % 7 == 0):

var results: [Int] = []

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

    // End code 
}

I hope this helps. Let me know if you have any questions :)

Michael Bau
Michael Bau
3,394 Points

Hi Alexandra,

Thanks a lot for your quick reply! That worked perfectly!

I must admit I had forgotten all about remainders!

Best regards, Michael