w3resource

Ruby Basic Exercises: Check whether a number is within 10 of 100 or 200

Ruby Basic: Exercise-21 with Solution

Write a Ruby program to check whether a number is within 10 of 100 or 200.

Ruby Code:

def near_hundred(n)
    (n-100).abs <= 10 || (n-200).abs <= 10
end

print near_hundred(10),"\n" 
print near_hundred(110),"\n" 
print near_hundred(90)

Output:

false
true
true

Flowchart:

Flowchart: Check whether a number is within 10 of 100 or 200

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to find the greatest of three numbers.
Next: Write a Ruby program to compute the sum of the two integers, if the two values are equal return double their sum otherwise return their sum.

What is the difficulty level of this exercise?