Ruby Array Exercises: Check whether a given array of integers of length 2 contains a 4 or a 7
Ruby Array: Exercise-17 with Solution
Write a Ruby program to check whether a given array of integers of length 2 contains a 4 or a 7.
Ruby Code:
def check_array(nums)
if(nums[0] == 4 || nums[0] == 7)
return true
end
return (nums[1] == 4 || nums[1] == 7)
end
print check_array([1, 4]),"\n"
print check_array([7, 5]),"\n"
print check_array([5, 2])
Output:
true true false
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to concatenate array of arrays into one.
Next: Write a Ruby program to check whether a given array of integers of length 2 does not contain a 6 or a 9.
What is the difficulty level of this exercise?
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/array/ruby-array-exercise-17.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics