w3resource

Ruby Basic Exercises: Check a given non-negative number and return true if num is within 2 of a multiple of 10

Ruby Basic: Exercise-46 with Solution

Write a Ruby program to check a given non-negative number and return true if num is within 2 of a multiple of 10.

Ruby Code:

def check_num(num)
   return num % 10 <= 2 || num % 10 >= 8;
end
print check_num(9),"\n"
print check_num(13),"\n"
print check_num(22),"\n"
print check_num(-12),"\n"
print check_num(0)

Output:

true
false
true
true
true

Flowchart:

Flowchart: Check a given non-negative number and return true if num is within 2 of a multiple of 10

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to check two given integers and return 11 if either one is 11. Return their sum or difference if sum or difference is 11.
Next: Write a Ruby program to check three given integers and return true if it is possible to add two of the integers to get the third.

What is the difficulty level of this exercise?