Enter a positive integer. That integer is The difference from multiples of 10 (10,20,30 ...) True if it is within 2 Other than that, let's display that the difference from a multiple of 10 is ○.
Output example:
near_ten(12)→True
near_ten (17) → The difference from a multiple of 10 is 3.
near_ten (23) → The difference from a multiple of 10 is 3.
def near_ten(num)
remainder = num % 10
if remainder <= 2 || remainder >= 8
puts "True"
elsif remainder <= 5
puts "The difference from multiples of 10#{remainder}is"
else
puts "The difference from multiples of 10#{10 - remainder}is"
end
end
I could answer this with a simple conditional expression, but I couldn't answer because I couldn't soften my head. The division of% is an image that appears in the drill unexpectedly.
(1) Divide an arbitrary number by 10 and substitute the remainder for remainder.
(2) If the conditional expression is if or less, puts "True" is displayed when the remainder is 2 or less or 8 or more.
③ If elsif is 5 or less, puts "The difference from a multiple of 10 is # {remainder}" is displayed.
④ In other cases (too much 6 or 7), puts "The difference from a multiple of 10 is # {10 -remainder}" is displayed.
This time, the content was understandable by looking at the code so that no explanation was necessary. However, if there is a similar problem next time, it will be bad if it cannot be solved.
Recommended Posts