w3resource

Ruby Basic Exercises: Check three given integers and return true if the three values are evenly spaced

Ruby Basic: Exercise-55 with Solution

Write a Ruby program to check three given integers (one of them is small, one is medium and one is large) and return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.

Ruby Code:

def check_num(a, b, c)
    if(b > a)
		temp = a
		a = b
		b = temp
    end

    if(c > b)
		temp = b
		b =c
		c = temp
    end

	if(b > a)
		temp = a
		a = b
		b = temp
	end
  return(a - b == b - c)
end
print check_num(5, 10, 15),"\n"
print check_num(2, 3, 11),"\n"

Output:

true
false

Flowchart:

Flowchart: Check three given integers and return true if the three values are evenly spaced

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to check three given integers x, y, z and return true if one of y or z is close (differing from a by at most 1), while the other is far, differing from both other values by 3 or more.
Next: Ruby Array Exercises

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