Today we will solve this problem.
Question 1 Enter a positive integer. That integer is True if the difference from a multiple of 10 (10,20,30 ...) is within 2. Other than that, False Let's create a method that outputs.
Output example: near_ten(12)→True near_ten(17)→False near_ten(19)→True
Model answer
def near_ten(num)
quotient = num % 10
if quotient <= 2 || quotient >= 8
puts "True"
else
puts "False"
end
end
Commentary To think about "differences from multiples of 10", focus on the ones digit. In other words, if the ones place is any of "0,1,2,8,9", it can be judged that "the difference from the multiple of 10 is within 2".
Therefore, assign the ones digit value to the variable quote and quote<= 2 || quotient >=8 is "0",1,2,8,We are checking if any of the 9's are true.
Recommended Posts