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:
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?
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-21.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics