Ruby Basic Exercises: Check two integer values and return true if they are both in the range 10..20
Ruby Basic: Exercise-37 with Solution
Write a Ruby program to check two integer values and return true if they are both in the range 10..20 inclusive, or they are both in the range 20..30 inclusive.
Ruby Code:
def test_int(a, b)
return (((a >= 10 && a <= 20) && (b >= 10 && b <= 20)) ||
((a >= 20 && a <= 30) && (b >= 20 && b <= 30)));
end
print test_int(10, 15),"\n"
print test_int(6, 9),"\n"
print test_int(22, 30),"\n"
print test_int(45, 55)
Output:
true false true false
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check two integers and return whichever value is nearest to the value 10, or return 0 if two integers are equal.
Next: Write a Ruby program to check two positive integer values and return the larger value that is in the range 20..30 inclusive, or return 0 if no number is in that range.
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-37.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics