w3resource

Ruby Basic Exercises: Compute the sum of the two integers

Ruby Basic: Exercise-22 with Solution

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 Basic Exercises: Compute the sum of the two integers

Ruby Code:

def sum_double(x, y)
    x == y ? (x+y)*2 : x+y
end

print sum_double(5, 5),"\n" 
print sum_double(4, 5)

Output:

20
9

Flowchart:

Flowchart: Compute the sum of the two integers

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to check if a number is within 10 of 100 or 200.
Next: Write a Ruby program to print "Ruby Basic Exercises" 9 times.

What is the difficulty level of this exercise?