Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Optionals Exercise: isDivisible function

Jan Francírek
Jan Francírek
1,766 Points

Why is it equal to 0 ? Thank you

Thank you for answer.

Joshua Blair
Joshua Blair
2,064 Points

% is for displaying remainders. So if something perfectly divides into another it should return 0.

4 divided by 2 = 2 ( 4 % 2 = 0 ) 5 divided by 2 = 2 with 1 remainder ( 5 % 2 = 1 )

Prateek Pamecha
Prateek Pamecha
2,482 Points

Equal to 0 means its completely divisible. "%" operator returns the remainder when a dividend is divided by divisor. For example:

10 (dividend) % 3 (Divisor) will return 1 as Remainder. This is because

10 (dividend) = 3(Divisor) * 3 (Quotient) + 1 (Remainder)

So if a number is not completely divisible then "%" operator will return some value as there will be something in remainder.

Example of Completely Divisible

10 (dividend) % 2 (Divisor) will return 0 as Remainder. This is because

10 (dividend) = 2 (Divisor) * 5 (Quotient) + 0 (Remainder)

I hope this answers why we check for is equal to 0 for checking if number is completely divisible.

1 Answer

Jan Francírek
Jan Francírek
1,766 Points

Thank you both of you :)