w3resource

Ruby Basic Exercises: Check three given integers and return true if one of them is 20 or more less than one of the others

Ruby Basic: Exercise-49 with Solution

Write a Ruby program to check three given integers and return true if one of them is 20 or more less than one of the others.

Ruby Code:

def check_num(a, b, c)
    return ((a - b).abs >= 20 || (b - c).abs >= 20 || (c - a).abs >= 20)
end

print check_num(9, 12, 22),"\n"
print check_num(112, 202, 20),"\n"
print check_num(102, 203, 405)

Output:

false
true
true

Flowchart:

Flowchart: Check three given integers and return true if one of them is 20 or more less than one of the others

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to check three given integers and return true if two or more of them have the same rightmost digit.
Next: Write a Ruby program to check two given integers and return the larger value. However if the two values have the same remainder when divided by 5 then return the smaller value and if the two values are the same, return 0.

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-49.php