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
Mike Cho
570 PointsSwift Basic Course Extra Credit - Solution
Hello,
I know the following is annoyingly simple to most and probably embarrassing to ask...but here we go anyways:
In the extra credits sections the task was:
"Given a range of numbers from 1 to 100. Write a loop which prints out whether a number is a odd or even."
I wasn't sure whether I'm doing the right thing, so here is what I wrote:
let numbers = 1...100
for number in numbers {
if (number % 2 == 0) {
println("even")
} else if (number % 2 != 0) {
println("odd")
}
}
In the console the output seems to be correct, but is there a better way to solve this?
The community is so overwhelmingly helpful here. Working hard to help others as well one day. Thank you!
7 Answers
Jeremy Hayden
1,740 PointsMike, that was a very good answer, and it works. That is the main thing.
Just a of suggestion though.
Your output would be more meaningful if it let us know which numbers it was talking about when it says "odd" or "even". You could use a skill that you have already learned, string interpolation, to make the output say something similar to "3 is odd", "4 is even".
But other than that, your code is clean and logical.
Mike Cho
570 PointsThat's true! I will try that out now. Thank you =) !
Jeremy Hayden
1,740 PointsJacob Your code looks good as well. However your last else statement is not needed.
Let's go thru your code.
First your counting from 1-100.
Then your dividing each number by 2.
Let's skip 1 for a second and go directly to 2. 2 divided by 2 = 1. It divides perfectly, there is nothing left over. So your remainder is zero. So your first if statement is true, the remainder is zero, so it prints out 2 is even.
Next number is 3. 3 divided 2 is 1.5. Or 1 with a remainder of 1 that can be further divided to the .5
We don't care about further dividing to get the decimal. We just want to know if there is a remainder or not.
So
4 / 2 = 2 remainder 0
5 / 2 = 2 remainder 1
6 / 2 = 3 remainder 0
7 / 2 = 3 remainder 1
Ect. Even numbers divided by 2 will always have remainder 0.
Back to your code...
Let's say your next number is 3
3/2=1 remainder 1, or %=1
So the first if statement is false and it moved on
Next
3/1=3 with no remainder, or %=0 so this is true and it prints out 3 is odd
Well since whole numbers are either even or odd that means the last if statement will NEVER get executed because one of the 2 first ones always will be true. So the last if statement is useless.
Next, do you really even need the second if statement? I mean if your number is not even, can't we just assume it's odd?
So instead f an if statement, you could just do, else - print odd
Jake Webb
13,028 PointsI really appreciate you breaking it down for me and helping me see things from a different perspective! Super Helpful!
Vasco Marques
938 PointsVery good explanation! Thanks Jeremy.
Jake Webb
13,028 PointsHere is my attempt..
for n in 1...100 {
if n % 2 == 0{
println("\(n) is Even")
} else if n % 1 == 0 {
println("\(n) is Odd")
} else {
println(n)
}
}
Could someone explain my logic? I don't understand it but I'm received the desired output. I'm just not able to understand remainders for some reason.
Jason Hernandez Berland
1,575 PointsSexy solution Jacob!
Looks like you understand very well from your code. Your conditional on lines 2 and 4 are checking for remainders:
if n % 2 == 0...
} else if n % 1 == 0 {...
So, is there a remainder when 1 % 2? Yep.
It does not satisfy == 0, so let's move on to the next condition...
is there a remainder when 1 % 1? No.
So it satisfies n % 1 == 0, and therefore prints output "1 is Odd"
ayushagarwal2
982 Pointsfor num in 1...100{
if (num%2==0)
{
println ("Even")
}else
{
println ("Odd")
}
}
Jonny Mack
497 PointsHere is my attempt. I got the desired results, but I have a little bit different code than everyone else
for number in 1...100{ if (number % 2 == 0){ println("(number) is Even") } else { println("(number) is Odd") } }
kols
27,007 PointsMy answer (I think this also should answer Lester's question above^^):
for i in 1...100 {
if (i % 2 == 0) {
println("\(i) is Even")
} else {
println("\(i) is Odd")
}
}
alex3
5,387 PointsThis is my answer
for n in 1...100 {
if n % 2 == 0 {
println("\(n) is odd")
} else if n % 3 == 0 {
println("\(n) is even")
} else {
println(n)
}
}
Lester Cerda
Courses Plus Student 567 PointsLester Cerda
Courses Plus Student 567 PointsIm with you Mike, i wasn't to sure either. This is my answer... It does show odds and even with numbers but i want the numbers to be first then odd or even eg.. 1 Odd not Odd 1.. any ideas???
import UIKit
for number in 1...100 { if number % 2 == 0{ println("Even (number)") } else { println("Odd (number)") }
}