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 Basics (retired) Control Flow Exercise: FizzBuzz Generator

Why the remainder operator(%) and not the division operator(/)?

I'm confused as to why the remainder operator is used and not the division.Thanks in advance for any help

1 Answer

The remainder operator means you don't have to do much math to solve this problem!

Think about how the remainder operator works. It returns only the remainder from dividing one number into another. It doesn't care how big the numbers are, only if there is a remainder.

For the FizzBuzz problem, you're trying to find numbers that are evenly divisible by 3 or 5. A number is evenly divisible if there is no remainder after dividing. So if number % 3 == 0, you know that number is evenly divisible by 3, without even checking what number is equal to.

It is possible to solve the problem using the regular division operator, but you'd have to add a few extra steps to see if there is a decimal portion left over. Remainder does this in one step.

thank you so much Sara.Makes great sense now.You're awesome