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:
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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework