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

Sapho Maqhwazima
Sapho Maqhwazima
805 Points

I know I'm missing something, but can anyone tell me why we do we type n % 7 == 0. I understand the n % 7 part.

I know I'm missing something, but can anyone tell me why we do we type n % 7 == 0. I understand the n % 7 part, whats the story with the 0? Can someone please break this down for me.

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

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

1 Answer

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

Hi Sapho for that bit of code its purpose is to see if you have two numbers that are perfectly divisible. n is representing the 100 numbers were going to be looping through and checking. To see if two numbers are completely divisible they will have a remainder of 0. 25/5 is perfectly divisible because you have no remainder and 5 goes into 25 five times.

let example = 25 % 5



if example == 0 {
    print(true)

} else {
    print(false)

}

example == 0

In the code when you write out 25 % 5 == 0 you write out twenty five divided by five is equal to zero and sure enough thats a true statement. For the FizzBuzz problem you need this to check if a number is perfectly divisible by 3 or 5.