w3resource

Ruby Basic Exercises: Check three given integers and return true if it is possible to add two of the integers to get the third

Ruby Basic: Exercise-47 with Solution

Write a Ruby program to check three given integers and return true if it is possible to add two of the integers to get the third.

Ruby Code:

def check_num(a, b, c)
   return ((a + b) == c || (b + c) == a || (c + a) == b)
end

print check_num(9, 12, 21),"\n"
print check_num(0, -5, -5),"\n"
print check_num(-5, 7, 2),"\n"
print check_num(6, 5, 4)

Output:

true
true
true
false

Flowchart:

Flowchart: Check three given integers and return true if it is possible to add two of the integers to get the third

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to check a given non-negative number and return true if num is within 2 of a multiple of 10.
Next: Write a Ruby program to check three given integers and return true if two or more of them have the same rightmost digit.

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