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 trialHarry Hendel
Courses Plus Student 2,748 PointsChecking to be odd and a multiple of 7
I am not sure how to check whether the value of the variable is a multiple of 7 and is odd
var results: [Int] = []
for n in 1...100 {
// Enter your code below
// End code
}
1 Answer
Robert Bojor
Courses Plus Student 29,439 PointsHi,
You can use the modulo calculation to establish if a number is a multiple of another number. The operator for this is % and the result of a modulo calculation will be 0 if it's a multiple (divides perfectly) or > 0 if it's not ( division has a remainder.
The code should look like this:
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n%7 == 0 {
print("\(n) is odd and multiple of 7") // Added just for effect
results.append(n)
}
// End code
}