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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby-exercises/basic/ruby-basic-exercise-46.php