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
Kevin Frostad
3,483 PointsOdd or Even challenge
I got the code to print out what numbers are odd and what numbers are even, but I don't really understand how it works.
if (numbers % 2) == true; will check how many times two goes in to each number from 1...100 right? So why does it print out that 1 is Odd? Two does not go into 1.... I just don't get it. Can anyone help to make the code make more sense? :)
CODE:
for numbers in 1...100 {
if (numbers % 2) == true {
println("\(numbers) is Odd")
} else if (numbers % 2) == false {
println("\(numbers) is Even")
} else {
println(numbers)
}
}
Kevin Frostad
3,483 PointsCaleb Kleveter. It doesn't work. I add " swift " above the code and " " under the code. And now the dots wont show....
Caleb Kleveter
Treehouse Moderator 37,862 PointsThere is a button underneath Esc or escape button that will create the back sticks.
Kevin Frostad
3,483 Pointsstill wont work.. Just adds thicker text. I use a macbook pro. The back sticks is to the left of my backspace, while I hold shift.
Caleb Kleveter
Treehouse Moderator 37,862 PointsI'm not sure what to do for you now. Sorry.
2 Answers
Steve Hunter
57,712 PointsHiya,
The % operator gives you the remainder after the division. That's not clear ...
10 % 2 = 0 - this is because 10/2 = 5 with no remainder.
10 % 3 = 1 - this is because 10/3 = 9 remainder 1.
10 % 4 = 2 - this is because 10/4 = 2 remainder 2.
The % gives you the remainder. So, to see if a number is even, the % should be zero when the number is divided by 2.
Your code should probably look like:
for numbers in 1...100 {
if (numbers % 2 == 0) { println("\(numbers) is Even") }
else {
if (numbers % 2 != 0) { println("\(numbers) is Odd")}
}
}
The last test is superfluous as the two are mutually exclusive, but you can see the point.
Hope that helps.
Kevin Frostad
3,483 PointsThanks! That makes a lot more sense.
Marileen Mennerich
1,161 PointsThe %-operator does not check how often a number fits into another, it returns to you the remainder that is left after putting the number into the other as opten as possible. So, any odd number % 2 will return 1, since if you take 2 out of an odd number, you will always be left with 1 remainder in the end. You can read it like this: x % 2 => x = y * 2 + z, where z is the result of the operation. 1 % 2 is 0 * 2 + 1, 3 % 2 is 1 * 2 + 1, and so on.
The code then compares the result to true or false, since, in programming languages, 1 often equals true, while 0 equals false.
Kevin Frostad
3,483 PointsThank you so much! :)
Caleb Kleveter
Treehouse Moderator 37,862 PointsCaleb Kleveter
Treehouse Moderator 37,862 PointsCan you add back sticks to your code? Or what I did below.
```c This goes above the code
``` This goes below the code
If you post code of a different language other then C switch the c out with the name of the language, make sure it's lower case.