I am reviewing the drill to strengthen my logical thinking. As a beginner, I would appreciate it if you could let me know if you have any questions.
True if the integer entered is less than 2 from a multiple of 10. Other than that, let's create a method that outputs False.
def near_ten(num)
first_place = num % 10
if first_place <= 2 || first_place >= 8
puts "True"
else
puts "False"
end
end
Focus on the ones place value.
"The difference from a multiple of 10 is within 2" means that the ones place is one of "0,1,2,8,9".
Therefore, use the modulo %
of the four arithmetic operations to get the ones digit, and substitute it for first_place
.
Andfirst_place <= 2 || first_place >= 8
so「0,1,2,8,9」
I am checking if it applies to.
There are other ways like this! If anyone can tell me, I would appreciate it if you could point it out! !!
Recommended Posts