w3resource

Ruby Basic Exercises: Check two given integers and return the larger value

Ruby Basic: Exercise-50 with Solution

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.

Ruby Basic Exercises: Check two given integers and return the larger value

Ruby Code:

def check_num(a, b)
   if(a == b)
		return 0
	end	
	if(a % 5 == b % 5)
		return (a < b) ? a : b
	end
	return (a > b) ? a : b
end

print check_num(9, 12),"\n"
print check_num(110, 200),"\n"
print check_num(10, 10)

Output:

12
110
0

Flowchart:

Flowchart: Check two given integers and return the larger value

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: 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.
Next: Write a Ruby program to check two given integers, each in the range 10..99, return true if there is a digit that appears in both numbers.

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://w3resource.com/ruby-exercises/basic/ruby-basic-exercise-50.php