w3resource

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


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

Go to:


PREV : 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.

Ruby Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.